如何使用回收站视图定制抽屉布局?

时间:2019-12-07 09:00:32

标签: android android-recyclerview drawerlayout

我想创建一个自定义的抽屉布局并在其中包含一个回收站视图,我该怎么做,以便所有回收站视图行都显示在抽屉布局内并使之可滚动,即我想显示整个{{1} }。

我的抽屉布局

FoodListFragment

我的回收站视图位于片段中

<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">


<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

2 个答案:

答案 0 :(得分:1)

您可以通过在DrawerLayout内添加片段来实现。检查以下内容:

首先,在DrawerLayout

中为您的片段创建一个容器
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <FrameLayout
            android:layout_gravity="start"
            android:id="@+id/main_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>


    <FrameLayout
        android:id="@+id/drawer_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true" />

</androidx.drawerlayout.widget.DrawerLayout>

然后,使用FragmentTransaction

将片段附加到此容器中
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new FoodListFragment());
fragmentTransaction.commit();

答案 1 :(得分:0)

在抽屉布局中添加以下代码

<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:menu="@menu/navigation_menu"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header"
    android:id="@+id/nv">

</android.support.design.widget.NavigationView>

在您的MainActivity中

private NavigationView nv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    nv = (NavigationView)findViewById(R.id.nv);
    nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            int id = item.getItemId();
            switch(id)
            {
                case R.id.account:
                    Toast.makeText(MainActivity.this, "My Account",Toast.LENGTH_SHORT).show();break;
                case R.id.settings:
                    Toast.makeText(MainActivity.this, "Settings",Toast.LENGTH_SHORT).show();break;
                case R.id.mycart:
                    Toast.makeText(MainActivity.this, "My Cart",Toast.LENGTH_SHORT).show();break;
                default:
                    return true;
            }


            return true;

        }
    });