簡體   English   中英

如何使用回收器視圖自定義抽屜布局?

[英]how to have a custom drawer layout with recycler view?

我想創建一個自定義的抽屜布局並在其中有一個回收器視圖,我該如何做到這一點,以便所有回收器視圖行都顯示在抽屜布局內並使其可滾動,即我想在抽屜中顯示整個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" />

您可以通過在DrawerLayout添加片段來實現這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();

在您的抽屜布局中添加以下代碼

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

在你的主活動中

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;

        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM