如何在嵌套的
NestedScrollView
内实现 recyclerview
的分页?NestedScrollView
内实现 recyclerview
的分页?按照以下步骤操作:
1. 将RecyclerView的“嵌套滚动”功能设置为false。
recyclerView.setNestedScrollingEnabled(false);
2. 给嵌套的滚动视图添加滚动监听器。
mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged()
{
View view = (View)mScrollView.getChildAt(mScrollView.getChildCount() - 1);
int diff = (view.getBottom() - (mScrollView.getHeight() + mScrollView
.getScrollY()));
if (diff == 0) {
// your pagination code
}
}
});
如果您正在使用Kotlin,则您的代码将如下所示:
scroll?.viewTreeObserver?.addOnScrollChangedListener {
val view = scroll.getChildAt(scroll.childCount - 1)
Timber.d("Count==============${scroll.childCount}")
val diff = view.bottom - (scroll.height + scroll.scrollY)
Timber.d("diff==============$diff")
if (diff == 0) {
//your api call to fetch data
}
}
最后但并非最不重要的,将RecyclerView的滚动设置为false
ViewCompat.setNestedScrollingEnabled(recyclerView, false)
我可以通过在nestedScrollView
中设置OnScrollChangeListener
来获取解决方案。
字段isLoading
应该在每次加载项目时更改,例如如果您正在使用Retrofit。您可以在其开始运行时将其设置为true
,并在获得response
或failure
时将其设置为false
。
字段isLastPage
应该在每次获取项目时更改,并检查是否是最后一页。
我正在使用Kotlin。
private var isLoading = false
private var isLastPage = false
nestedScrollView.setOnScrollChangeListener { v: NestedScrollView?, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int ->
val nestedScrollView = checkNotNull(v){
return@setOnScrollChangeListener
}
val lastChild = nestedScrollView.getChildAt(nestedScrollView.childCount - 1)
if (lastChild != null) {
if ((scrollY >= (lastChild.measuredHeight - nestedScrollView.measuredHeight)) && scrollY > oldScrollY && !isLoading && !isLastPage) {
//get more items
}
}
}
当然,你需要将字段 isNestedScrollingEnabled
设置为 false
myRecyclerView.isNestedScrollingEnabled = false
我曾经遇到过同样的问题,根据 Android 文档:
永远不要将 RecyclerView 或 ListView 添加到滚动视图中。这样做会导致用户界面性能差和用户体验差。
我使用下面的代码解决了我的问题:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar_layout"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:titleEnabled="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Put here elements that you need above the recycler view -->
</LinearLayout>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<!-- RecyclerView -->
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:scrollbars="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>