抽屉控件——DrawerLayout

本文介绍如何使用Android的DrawerLayout和NavigationView实现侧滑菜单功能。详细展示了XML布局配置及Java代码中菜单项点击事件处理和Fragment切换的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值