我正在使用 RecyclerView
并以十个为一组从 API 中获取对象。对于分页,我使用 EndlessRecyclerOnScrollListener
.
一切正常。现在剩下的就是在列表底部添加一个进度微调器,同时 API 获取下一批对象。这是 Google Play 商店应用程序的屏幕截图,在肯定是 RecyclerView
中显示了一个 ProgressBar
:
问题是,RecyclerView
和 EndlessRecyclerOnScrollListener
都没有内置支持在下一批时在底部显示 ProgressBar
正在获取对象。
我已经看到了以下答案:
1. Put an indeterminate ProgressBar
as footer in a RecyclerView
grid .
2. Adding items to Endless Scroll RecyclerView
with ProgressBar
at bottom .
我对这些答案不满意(都是同一个人)。这涉及在用户滚动时将 null
对象硬塞到数据集中,然后在下一批交付后将其取出。它看起来像一个 hack,它回避了可能正常工作也可能不正常工作的主要问题。它会在列表中造成一些不和谐和失真
使用 SwipeRefreshLayout
不是这里的解决方案。 SwipeRefreshLayout
涉及从顶部拉取 最新 项,无论如何它不会显示进度 View 。
有人可以为此提供一个好的解决方案吗?我很想知道谷歌是如何为他们自己的应用程序实现的(Gmail 应用程序也有)。有没有详细介绍过的文章?所有答案和评论将不胜感激。谢谢。
其他一些引用资料:
1. Pagination with RecyclerView
. (精湛的概述......)
2. RecyclerView
header and footer . (更多相同...)
最佳答案
这是更简单、更清洁的方法。
从 this Codepath Guide 实现无限滚动然后按照以下步骤操作。
1.在 RecyclerView 下添加进度条。
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_movie_grid"
android:layout_width="0dp"
android:layout_height="0dp"
android:paddingBottom="50dp"
android:clipToPadding="false"
android:background="@android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</android.support.v7.widget.RecyclerView>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:background="@android:color/transparent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
这里 android:paddingBottom="50dp" 和 android:clipToPadding="false" 非常重要。
<强>2。获取对进度条的引用。
progressBar = findViewById(R.id.progressBar);
3.定义显示和隐藏进度条的方法。
void showProgressView() {
progressBar.setVisibility(View.VISIBLE);
}
void hideProgressView() {
progressBar.setVisibility(View.INVISIBLE);
}
关于android - Endless RecyclerView 和 ProgressBar 用于分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34431734/