DrawerLayout侧滑详简总结

一、DrawerLayout简介

       DrawerLayout是抽屉侧滑导航栏,以隐藏状态布局在屏幕边缘。用户从屏幕边缘滑动,菜单栏就会显示出来。DrawerLayout还是容器ViewGroup,一般包含RelativeLayout(相对布局)+FrameLayout两个子布局。

二、DrawerLayout侧滑xml布

        主要重点布局:DrawerLayout+RelativeLayout+FrameLayout,在DrawerLayout中RelativeLayout布局一定要设置android:layout_gravity=""(start\左,end\右)属性,这不是DrawerLayout本身的属性,而是其子视图的属性,决定子视图作为抽屉滑动的方向。

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@drawable/p1">
    <FrameLayout
        android:id="@+id/fragment_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="124dp"
            android:text="首页"
            android:textColor="#FFEB3B"
            android:layout_gravity="end"
            android:textSize="56dp" />
    </FrameLayout>
    <RelativeLayout
        android:id="@+id/relative_layout"
        android:layout_width="290dp"
        android:background="#49AAEA"
        android:layout_height="match_parent"
        android:layout_gravity="start">
        <ListView
            android:id="@+id/list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:listSelector="#26CBDA">
        </ListView>
    </RelativeLayout>
</androidx.drawerlayout.widget.DrawerLayout>

三,新建FirstFragment类继承Fragment作为侧滑菜单相对应的页面

        first_activity.xml为FirstFragment页面的xml布局文件,可以在first_activity.xml布局相应的需求。

public class FirstFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.first_activity,container,false);
    }
}

四,对ListView的项作点击事件的监听

        setOnItemClickListener设置ListView的Item项监听,drawerLayout.closeDrawer(relativeLayout_start)关闭作为抽屉的RelativeLayout布局。

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                Fragment fragment = null;
                switch (position){
                    case 0:
                        fragment = new FirstFragment();
                        break;
                    case 1:
                        fragment = new SecondFragment();
                        break;
                    default:
                        break;
                }
                ft.replace(R.id.fragment_layout,fragment);
                ft.addToBackStack(null);
                ft.commitAllowingStateLoss();
                drawerLayout.closeDrawer(relativeLayout_start);
            }
        });

五,最终代码

public class MainActivity extends AppCompatActivity {
    private static final String[] list_item = {"第一","第二"};
    private DrawerLayout drawerLayout;
    private RelativeLayout relativeLayout_start;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        drawerLayout = findViewById(R.id.drawer_layout);
        relativeLayout_start = findViewById(R.id.relative_layout);
        ListView listView = findViewById(R.id.list_view);
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,list_item));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                Fragment fragment = null;
                switch (position){
                    case 0:
                        fragment = new FirstFragment();
                        break;
                    case 1:
                        fragment = new SecondFragment();
                        break;
                    default:
                        break;
                }
                ft.replace(R.id.fragment_layout,fragment);
                ft.addToBackStack(null);
                ft.commitAllowingStateLoss();
                drawerLayout.closeDrawer(relativeLayout_start);
            }
        });
    }
}

六,实现效果

DrawerLayout抽屉侧滑导航

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值