简体   繁体   中英

how to make a recycler view scrollable

I am trying to make a recycler view scrollable in my xml layout file but it does not scroll

here is what I have tried

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <data class=".ProductsBinding">
        <variable
            name="productsViewModel"
            type="com.xxx.xx.ProductsViewModel" />
    </data>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ProductsFragment">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/products_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    </androidx.core.widget.NestedScrollView>
</LinearLayout>
</layout>

Can you please suggest how to get this to work please

thanks R

first, if you have only RecyclerView as child, why use NestedScrollView??

anyway how about this?

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <data class=".ProductsBinding">
        <variable
            name="productsViewModel"
            type="com.xxx.xx.ProductsViewModel" />
    </data>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ProductsFragment">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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


            <androidx.recyclerview.widget.RecyclerView
               android:id="@+id/products_recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            </LinearLayout>
    </androidx.core.widget.NestedScrollView>
</LinearLayout>
</layout>

add linearLayout in NestedScrollView

CF] If you use a recycler view within a double scroll view, the recycler view will generate all the items in advance. (You can check the log from the onBindViewHolder on the RecycleView Adapter.) Also, because we don't recycle Item View, we lose the big advantage of Recycling View, which can increase memory efficiency by reusing View, so be sure to keep that in mind. If you have a lot of items, avoid using them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM