xml中设置
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/drawerLayout">
<!--主布局-->
<FrameLayout
android:id="@+id/framLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<!--侧滑布局-->
<android.support.design.widget.NavigationView
android:id="@+id/left_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="left"
app:headerLayout="@layout/headlayout"
app:menu="@menu/menu">
</android.support.design.widget.NavigationView>
说明
app:headerLayout:侧滑头部的布局
app:menu:侧滑列表的引用(menu资源文件)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item
android:id="@+id/home"
android:icon="@mipmap/ic_home_white_24dp"
android:title="Home"/>
<item
android:id="@+id/book"
android:icon="@mipmap/ic_book_white_24dp"
android:title="Book"/>
<item
android:id="@+id/music"
android:icon="@mipmap/ic_music_note_white_24dp"
android:title="Music"/>
<item
android:id="@+id/movie"
android:icon="@mipmap/ic_tv_white_24dp"
android:title="Movie"/>
<item
android:id="@+id/favorite"
android:icon="@mipmap/ic_favorite_white_24dp"
android:title="favorite"/>
</group>
<item android:title="dfsd">
<menu>
<item
android:id="@+id/help"
android:icon="@mipmap/ic_menu_help"
android:title="Help"/>
<item
android:id="@+id/info"
android:icon="@mipmap/ic_menu_info_details"
android:title="Info"/>
<item
android:id="@+id/infos"
android:title=""/>
</menu>
</item>
</menu>
代码
public class DrawerLayoutActivity extends BaseActivity {
private DrawerLayout drawerLayout;
private NavigationView navigationView;
private FragmentManager manager;
List<Fragment> fragmentList=new ArrayList<>();
int curFragment=-1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_layout);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
navigationView = (NavigationView) findViewById(R.id.left_view);
manager = getSupportFragmentManager();
initFragment();
initDrawerView();
switchTab(0);
}
private void initFragment() {
fragmentList.add(new HomeFragment());
fragmentList.add(new BookFragment());
fragmentList.add(new MusicFragment());
fragmentList.add(new MovieFragment());
fragmentList.add(new FavoriteFragment());
}
private void initDrawerView() {
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int position=switchTag(item.getItemId());
if(position!=curFragment){
switchTab(position);
curFragment=position;
}
drawerLayout.closeDrawers();
return true;
}
});
}
private void switchTab(int position){
Fragment fragment = manager.findFragmentByTag("" + position);
FragmentTransaction beginTransaction = manager.beginTransaction();
if(fragment==null){
if(manager.findFragmentByTag("" + curFragment)!=null){
beginTransaction.hide(fragmentList.get(curFragment));
}
beginTransaction.add(R.id.framLayout,fragmentList.get(position),""+position)
.show(fragmentList.get(position))
.commit();
}else if(curFragment!=position){
beginTransaction.hide(fragmentList.get(curFragment))
.show(fragmentList.get(position))
.commit();
}
curFragment=position;
}
private int switchTag(int selectItemId){
switch (selectItemId){
case R.id.home:
return 0;
case R.id.book:
return 1;
case R.id.music:
return 2;
case R.id.movie:
return 3;
case R.id.favorite:
return 4;
}
return 0;
}
}
参考博客:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0925/1713.html