[英]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. 我正在使用NestedScrollView,在里面,我有一些编辑文本和按钮,然后是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. 当我打开活动时,所有项目都开始使用PageKeyedDataSource从服务器获取,而RecyclerView滚动非常缓慢,最后应用程序显示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. 我在android-architecture-components的GitHub repo中看到了这个问题,发现SmartNestedScrollView可以解决问题但是当我使用SmartNestedScrollView时,我的回收器视图中只有一小部分会滚动。
https://github.com/googlesamples/android-architecture-components/issues/215 https://github.com/googlesamples/android-architecture-components/issues/215
LayoutFile.xml 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. 我刚刚移动了整个视图,我希望在回收器视图中的回收器视图上方显示,即将该视图作为Recycler View的标题。
Inside the Recycler View : 在Recycler视图中:
const val HEADER = 0
const val REAL_ITEM = 1
The getItemViewType method which will return the view type to onCreateViewHolder. getItemViewType方法,它将视图类型返回到onCreateViewHolder。
override fun getItemViewType(position: Int): Int {
return when (position) {
0 -> HEADER
else -> REAL_ITEM
}
}
Inside the onCreateViewHolder method: 在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)
}
Inside the onBindViewHolder you can get the view type and bind data as per the view requires. 在onBindViewHolder中,您可以根据视图需要获取视图类型和绑定数据。
override fun onBindViewHolder(p0: NewsViewHolder, p1: Int) {
val viewType = getItemViewType(p1)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.