【发布时间】:2019-12-07 09:00:32
【问题描述】:
我想创建一个自定义抽屉布局并在其中有一个回收器视图,我该如何做到这一点,以便所有回收器视图行都显示在抽屉布局内并使其可滚动,即我想在其中显示整个 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" />
片段内的我的回收站视图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
tools:context=".FoodListFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
【问题讨论】:
-
您的意思是希望整个
FoodListFragment成为抽屉吗?还是你想要FoodListFragment里面的抽屉? -
是的,我希望
FoodListFragment成为抽屉。 -
好的,那么在
<DrawerLayout>里面,在主要内容之后,你可以简单地添加<fragment android:name="your.package.name.FoodListFragment" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start|left" />。该实例将为您自动加载,因此您无需在代码中为它执行任何FragmentTransaction。
标签: android android-recyclerview drawerlayout