SlidingMenu滑动菜单及DrawerLayout
publicclass MainActivity extends Activity {
private SlidingMenu slidingMenu;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
slidingMenu = new SlidingMenu(this);
slidingMenu.setMode(SlidingMenu.LEFT_RIGHT);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setMenu(R.layout.sm_left);
slidingMenu.setSecondaryMenu(R.layout.sm_right);
slidingMenu.setBehindWidth(200);
slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
}
}
一、MainActivity代码:
publicclass MainActivity extends FragmentActivity {
private SlidingMenu slidingMenu;
private ListView listView_menu_titlelist;
private MySQLiteDatabaseHelper dbHelper;
private List
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new MySQLiteDatabaseHelper();
totalList = dbHelper.selectData(
"select id ,title ,content from android_basic", null);
List
for (int i = 0; i < totalList.size(); i++) {
titleList.add(totalList.get(i).get("title").toString());
}
ArrayAdapter
android.R.layout.simple_list_item_1, titleList);
getSlidingMenu(adapter);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setHomeButtonEnabled(true);
// actionBar.setHomeAsUpIndicator(getResources().getDrawable(
// R.drawable.indicator));
}
privatevoid getSlidingMenu(ArrayAdapter adapter) {
slidingMenu = new SlidingMenu(this);
slidingMenu.setMode(SlidingMenu.LEFT);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
// slidingMenu.setTouchModeBehind(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setMenu(R.layout.slidingmenu_left);
slidingMenu
.setBehindWidth(getResources().getDisplayMetrics().widthPixels / 2);
slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
listView_menu_titlelist = (ListView) slidingMenu.getMenu()
.findViewById(R.id.listView_menu_titlelist);
listView_menu_titlelist.setAdapter(adapter);
listView_menu_titlelist
.setOnItemClickListener(new OnItemClickListener() {
@Override
publicvoid onItemClick(AdapterView> parent, View view,
int position, long id) {
Map
String data = map.get("content").toString();
ContentFragment fragment = new ContentFragment();
Bundle bundle = new Bundle();
bundle.putString("content", data);
fragment.setArguments(bundle);
FragmentTransaction transaction = MainActivity.this
.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.layout_container, fragment);
transaction.addToBackStack("androidManual");
transaction.commit();
}
});
}
@Override
publicboolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
@Override
publicboolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_showmenu:
slidingMenu.toggle();
break;
case android.R.id.home:
slidingMenu.toggle();
break;
default:
break;
}
returnsuper.onOptionsItemSelected(item);
}
}
二、ContentFragment代码:
publicclass ContentFragment extends Fragment {
private TextView textView_fragment_content;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_content, null);
textView_fragment_content = (TextView) view
.findViewById(R.id.text_fragment_content);
Bundle bundle = getArguments();
String data = bundle.getString("content");
textView_fragment_content.setText(data);
return view;
}
}