简体   繁体   中英

PagedListAdapter fetching all the items when recycler view is placed inside nestedscrollview

I am using a NestedScrollView and inside that, I am having some edit text and buttons and then a RecyclerView. When I open the activity all the items start fetching from the server using the PageKeyedDataSource and the RecyclerView scrolls very slowly and in the end the app show ANR.

I have looked this issue in the GitHub repo of android-architecture-components and found that SmartNestedScrollView can solve the problem but when I use SmartNestedScrollView then only a small portion of my recycler view scrolls.

https://github.com/googlesamples/android-architecture-components/issues/215

LayoutFile.xml

<?xml version="1.0" encoding="utf-8"?>
<com.magtappofficial.app.utilities.SmartNestedScrollView
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:layout_margin="16dp"
    tools:context=".ui.Home">

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_width="0px"
            android:layout_height="0px"/>


    <EditText.../>

    <ImageView.../>

    <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/newsProgress"
            android:layout_marginTop="16dp"
            app:layout_constraintTop_toBottomOf="@+id/exclusiveForYou"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"/>

    <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/newsRecycler"
            android:layout_marginTop="16dp"
            app:layout_constraintTop_toBottomOf="@+id/exclusiveForYou"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

</com.magtappofficial.app.utilities.SmartNestedScrollView>

Can Anyone Please tell me what else can I do?

Note: This is not the best practice, it's just a workaround and if someone has any better answer please please please share.

For those of you who are still looking for an answer, I have a simple fix.

I have just moved the entire view that I wanted to show above the recycler view inside the recycler view, ie, made that view as the header of Recycler View.

布局蓝图

Inside the Recycler View :

const val HEADER = 0
const val REAL_ITEM = 1

The getItemViewType method which will return the view type to onCreateViewHolder.

override fun getItemViewType(position: Int): Int {
        return when (position) {
            0 -> HEADER
            else -> REAL_ITEM
        }
    }

Inside the onCreateViewHolder method:

override fun onCreateViewHolder(p0: ViewGroup, p1: Int): NewsViewHolder {
        val view = when (p1) {
            HEADER -> {
                LayoutInflater.from(ctx).inflate(R.layout.rv_header, p0, false)
            }
            else -> LayoutInflater.from(ctx).inflate(R.layout.xyz, p0, false)
        }
        return SomeViewHolder(view)
    }

Inside the onBindViewHolder you can get the view type and bind data as per the view requires.

override fun onBindViewHolder(p0: NewsViewHolder, p1: Int) {
        val viewType = getItemViewType(p1)
}

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