簡體   English   中英

當回收器視圖放在nestedscrollview中時,PagedListAdapter獲取所有項目

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

我正在使用NestedScrollView,在里面,我有一些編輯文本和按鈕,然后是RecyclerView。 當我打開活動時,所有項目都開始使用PageKeyedDataSource從服務器獲取,而RecyclerView滾動非常緩慢,最后應用程序顯示ANR。

我在android-architecture-components的GitHub repo中看到了這個問題,發現SmartNestedScrollView可以解決問題但是當我使用SmartNestedScrollView時,我的回收器視圖中只有一小部分會滾動。

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>

任何人都可以告訴我還能做什么?

注意:這不是最佳做法,它只是一種解決方法,如果有人有任何更好的答案,請分享。

對於那些仍在尋找答案的人,我有一個簡單的解決方法。

我剛剛移動了整個視圖,我希望在回收器視圖中的回收器視圖上方顯示,即將該視圖作為Recycler View的標題。

布局藍圖

在Recycler視圖中:

const val HEADER = 0
const val REAL_ITEM = 1

getItemViewType方法,它將視圖類型返回到onCreateViewHolder。

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

在onCreateViewHolder方法中:

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

在onBindViewHolder中,您可以根據視圖需要獲取視圖類型和綁定數據。

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

暫無
暫無

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

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