直接上代码:
activity_main.xml:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 主视图 --> <FrameLayout android:id="@+id/content_layout" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> <!-- 左侧菜单 --> <ListView android:id="@+id/letf_menu" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#ffffcc" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp"> </ListView> </android.support.v4.widget.DrawerLayout>
MainActivity:
public class MainActivity extends Activity implements OnItemClickListener { private DrawerLayout mDrawerLayout; private ListView mListView; private List<String> menuList; private ArrayAdapter<String> adapter; private ActionBarDrawerToggle toggle; private String mTitle; //1.创建DrawerLayout并填充内容 //2.实现侧滑菜单的点击事件,点击后切换内容 //3.在导航条右侧显示搜索按钮,并实现打开网页功能 //4.实现当打开侧滑菜单后头部title的改变,并且隐藏其他menu;关闭侧滑菜单后显示Menu 变更title //5.实现点击左上角导航按钮打开侧滑菜单功能 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取当前的title mTitle=(String) getTitle(); //----------------1.-------------- // 为左侧抽屉菜单 添加内容 mDrawerLayout=(DrawerLayout) findViewById(R.id.drawer_layout); mListView=(ListView) findViewById(R.id.letf_menu); menuList=new ArrayList<String>(); for(int i=0;i<5;i++){ menuList.add("抽屉菜单0"+i); } adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuList); mListView.setAdapter(adapter); //----------------2.-------------- //当点击左侧菜单 时,添加监听事件 mListView.setOnItemClickListener(this); //----------------4.-------------- //为 DrawerLayout 设置 ActionBarDrawerToggle 监听事件,来完成监听抽屉状态变化的功能。 //当侧拉菜单打开以后,显示title为请选择; 关闭以后显示原title toggle=new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer , R.string.open_drawer,R.string.close_drawer){ @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); getActionBar().setTitle("请选择"); //重新绘制头部的Menu,当调用invalidateOptionsMenu时,系统会自动调用onPrepareOptionsMenu方法 invalidateOptionsMenu(); } @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); getActionBar().setTitle(mTitle); //重新绘制头部的Menu invalidateOptionsMenu(); } }; mDrawerLayout.setDrawerListener(toggle); //----------------5.-------------- //开启ActionBar 上面的icon的功能 getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeButtonEnabled(true); } //----------------4.-------------- @Override public boolean onPrepareOptionsMenu(Menu menu) { //获取侧拉菜单的打开状态 Boolean isOpen=mDrawerLayout.isDrawerOpen(mListView); //当侧拉菜单 打开时,隐藏搜索按钮,当侧拉菜单关闭时,显示搜索按钮 menu.findItem(R.id.action_search).setVisible(!isOpen); return super.onPrepareOptionsMenu(menu); } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { //----------------2.-------------- // 动态插入一个Fragment到FrameLayout中 Fragment fragment=new ContentFragment(); Bundle args=new Bundle(); args.putString("text", menuList.get(position)); fragment.setArguments(args); //替换掉当前的Fragment内容 FragmentManager manager=getFragmentManager(); manager.beginTransaction().replace(R.id.content_layout, fragment).commit(); //关闭左侧菜单 mDrawerLayout.closeDrawer(mListView); } @Override public boolean onOptionsItemSelected(MenuItem item) { //----------------5.-------------- //将ActionBar与Drawer结合起来,点击左上角的菜单时,打开侧拉菜单 if(toggle.onOptionsItemSelected(item)){ return true; } //----------------3.-------------- //当点击搜索按钮的时候打开一个网站 switch (item.getItemId()) { case R.id.action_search: Intent intent=new Intent(); intent.setAction("android.intent.action.VIEW"); Uri uri=Uri.parse("http://www.fitnes.cn"); intent.setData(uri); startActivity(intent); break; default: break; } return super.onOptionsItemSelected(item); } //----------------5.-------------- @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); //调用ActionBarDrawerToggle.syncState() //在Activity的onPostCreate()中;指示,ActionBarDrawerToggle与DrawerLayout的状态同步, //并将ActionBarDrawerToggle中的drawer图标,设置为ActionBar的Home-Button的icon toggle.syncState(); } //----------------5.-------------- @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); toggle.onConfigurationChanged(newConfig); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
ContentFragment:
public class ContentFragment extends Fragment { //----------------2.-------------- private TextView tv; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.content_fragment,container, false); tv=(TextView) view.findViewById(R.id.txtView); String text=getArguments().getString("text"); tv.setText(text); return view; } }
content_fragment.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- 2. --> <TextView android:id="@+id/txtView" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
menu/main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 3. --> <item android:id="@+id/action_search" android:showAsAction="ifRoom|withText" android:title="@string/webSerach" android:icon="@drawable/action_search"/> </menu>
ActionBarDrawerToggle 和DrawerLayout资料
http://www.tuicool.com/articles/NVzu6b6
http://blog.csdn.net/jjwwmlp456/article/details/38682637
效果: