一、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抽屉侧滑导航