我正在使用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
对象硬塞到数据集中,然后在下一批交付后将其取出。它看起来像是一个回避主要问题的黑客,可能会或可能不会正常工作。这会导致列表中出现一些不和谐和失真
使用SwipeRefreshLayout
不是这里的解决方案。 SwipeRefreshLayout
涉及从顶部拉取最新项目,并且无论如何它都不会显示进度 View 。
有人可以为此提供一个好的解决方案吗?我有兴趣了解 Google 如何在自己的应用程序中实现此功能(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 - 带 ProgressBar 的 Endless RecyclerView 用于分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61015576/