【问题标题】:how to have a custom drawer layout with recycler view?如何使用回收站视图进行自定义抽屉布局?
【发布时间】: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 成为抽屉。
  • 好的,那么在&lt;DrawerLayout&gt;里面,在主要内容之后,你可以简单地添加&lt;fragment android:name="your.package.name.FoodListFragment" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start|left" /&gt;。该实例将为您自动加载,因此您无需在代码中为它执行任何FragmentTransaction

标签: android android-recyclerview drawerlayout


【解决方案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();

【讨论】:

  • 请不要建议用户将其他&lt;View&gt;s 放在&lt;NavigationView&gt; 中。它真的不应该那样使用。 NavigationView 有一个非常具体的内部结构,它可以动态构建自己。此外,DrawerLayout 中的抽屉不一定是NavigationView。它几乎可以是您想要的任何ViewViewGroup,因此实际上没有必要将&lt;NavigationView&gt; 用作容器。
  • 谢谢你们,它成功了,但无法滚动!,我该如何解决这个问题?
  • @hiwajalal,我已经检查过了,它正在滚动。你有足够的项目来滚动吗?
  • @Md.Asaduzzaman 是的,我有很多要滚动的项目,这是我当前的抽屉布局pastebin.com/nYSYSf3J
  • 将您的drawer_fragment_container 放在DrawerLayout 的底部。检查我的更新答案
【解决方案2】:

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

<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;

        }
    });

【讨论】:

  • 我在哪里将片段添加到抽屉?
  • 在抽屉布局中添加框架布局
猜你喜欢
  • 2023-03-14
  • 1970-01-01
  • 2017-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多