android中的延迟加载recyclerView
有人知道如何在android中实现延迟加载回收视图吗?我是android新手,不知道如何解决这个问题。如果有人能帮助我,我将不胜感激。您可以使用EndlessRecyclerViewandroid中的延迟加载recyclerView,android,android-recyclerview,lazy-loading,Android,Android Recyclerview,Lazy Loading,有人知道如何在android中实现延迟加载回收视图吗?我是android新手,不知道如何解决这个问题。如果有人能帮助我,我将不胜感激。您可以使用EndlessRecyclerView 概述 一个常见的应用程序功能是在用户滚动项目时自动加载更多项目(也称为无限滚动)。这是通过当用户在到达终点之前超过剩余项目的阈值时触发对更多数据的请求来实现的 ListView、GridView和RecyclerView(ListView的继承者)的方法在这里有文档记录。除了需要传入RecyclerView中的La
概述 一个常见的应用程序功能是在用户滚动项目时自动加载更多项目(也称为无限滚动)。这是通过当用户在到达终点之前超过剩余项目的阈值时触发对更多数据的请求来实现的 ListView、GridView和RecyclerView(ListView的继承者)的方法在这里有文档记录。除了需要传入RecyclerView中的LayoutManager以提供实现无限滚动所需的信息之外,这两种方法的代码都类似 在这两种情况下,实现滚动所需的信息包括确定列表中最后一个可见项,以及在到达最后一个项之前开始获取更多数据的某种类型的阈值。此数据可用于决定何时从外部源加载更多数据: 使用ListView或GridView实现 每个AdapterView(如ListView和GridView)都支持绑定到OnScrollListener事件,每当用户在集合中滚动时就会触发这些事件。使用此系统,我们可以通过创建扩展OnScrollListener的类来定义支持大多数用例的基本EndlessScrollListener:
import android.widget.AbsListView;
public abstract class EndlessScrollListener implements AbsListView.OnScrollListener {
// The minimum number of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
// The current offset index of data you have loaded
private int currentPage = 0;
// The total number of items in the dataset after the last load
private int previousTotalItemCount = 0;
// True if we are still waiting for the last set of data to load.
private boolean loading = true;
// Sets the starting page index
private int startingPageIndex = 0;
public EndlessScrollListener() {
}
public EndlessScrollListener(int visibleThreshold) {
this.visibleThreshold = visibleThreshold;
}
public EndlessScrollListener(int visibleThreshold, int startPage) {
this.visibleThreshold = visibleThreshold;
this.startingPageIndex = startPage;
this.currentPage = startPage;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
// If the total item count is zero and the previous isn't, assume the
// list is invalidated and should be reset back to initial state
if (totalItemCount < previousTotalItemCount) {
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = totalItemCount;
if (totalItemCount == 0) { this.loading = true; }
}
// If it's still loading, we check to see if the dataset count has
// changed, if so we conclude it has finished loading and update the current page
// number and total item count.
if (loading && (totalItemCount > previousTotalItemCount)) {
loading = false;
previousTotalItemCount = totalItemCount;
currentPage++;
}
// If it isn't currently loading, we check to see if we have breached
// the visibleThreshold and need to reload more data.
// If we do need to reload some more data, we execute onLoadMore to fetch the data.
if (!loading && (firstVisibleItem + visibleItemCount + visibleThreshold) >= totalItemCount ) {
loading = onLoadMore(currentPage + 1, totalItemCount);
}
}
// Defines the process for actually loading more data based on page
// Returns true if more data is being loaded; returns false if there is no more data to load.
public abstract boolean onLoadMore(int page, int totalItemsCount);
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// Don't take any action on changed
}
}
现在,当您滚动时,项目将自动填充,因为一旦用户超过VisibleStorm,将触发onLoadMore方法。这种方法同样适用于GridView,侦听器允许访问页面和totalItemsCount,以支持分页和基于偏移量的抓取
使用RecyclerView实施
我们可以通过定义需要实现onLoadMore()方法的接口EndlessRecycleWebScrollListener,对RecycleView使用类似的方法。LayoutManager在RecyclerView中负责呈现项目应放置的位置并管理滚动,它提供了有关当前相对于适配器的滚动位置的信息。因此,我们需要传递一个LayoutManager用于收集必要信息的实例,以确定何时加载更多数据。
为RecyclerView实现无止境分页需要以下步骤:为RecyclerView实现无止境分页需要以下步骤:
1.将EndlessRecycleWebScrollListener.java复制到应用程序中。
2.在RecyclerView上调用addOnScrollListener(…)以启用无止境分页。传入EndlessRecycleViewScrollListener的实例,并实现onLoadMore,每当需要加载新页面以填充列表时,它都会触发该onLoadMore。
3.在前面提到的onLoadMore方法中,通过发送网络请求或从另一个源加载,将其他项目加载到适配器中。
要开始处理步骤2和步骤3的滚动事件,我们需要在活动或片段中使用addOnScrollListener()方法,并使用布局管理器传入EndlessRecycleServiceScrollListener实例,如下所示:
public class MainActivity extends Activity {
// Store a member variable for the listener
private EndlessRecyclerViewScrollListener scrollListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
// Configure the RecyclerView
RecyclerView rvItems = (RecyclerView) findViewById(R.id.rvContacts);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
rvItems.setLayoutManager(linearLayoutManager);
// Retain an instance so that you can call `resetState()` for fresh searches
scrollListener = new EndlessRecyclerViewScrollListener(linearLayoutManager) {
@Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
// Triggered only when new data needs to be appended to the list
// Add whatever code is needed to append new items to the bottom of the list
loadNextDataFromApi(page);
}
};
// Adds the scroll listener to RecyclerView
rvItems.addOnScrollListener(scrollListener);
}
// Append the next page of data into the adapter
// This method probably sends out a network request and appends new data items to your adapter.
public void loadNextDataFromApi(int offset) {
// Send an API request to retrieve appropriate paginated data
// --> Send the request including an offset value (i.e `page`) as a query parameter.
// --> Deserialize and construct new model objects from the API response
// --> Append the new data objects to the existing set of items inside the array of items
// --> Notify the adapter of the new items made with `notifyItemRangeInserted()`
}
}
我认为这样更好,因为您不需要特定版本的minsdk 步骤1:创建界面
interface caller{
void call();
}
if (position==items.size()-1){// its a last item
callbacker.call();
}
第2步:在RecycleView上创建呼叫者
步骤3:检查是否为最后一项?怎么做?
在onBindViewHolder上的Recyclerview中
interface caller{
void call();
}
if (position==items.size()-1){// its a last item
callbacker.call();
}
步骤4:在创建回收者视图的活动上定义和传递给您的回收者视图构造函数
callbacker call{
recyclerParamerList.addAll(ItemList items());
youradapter.notifyDataSetChanged();
}
在Kotlin中这很容易,只需使用惰性委托?cricket_007链接已重新定位无法读取的复制粘贴。。。如果没有其他要添加的内容,最好只复制链接;)顺便说一句:良好链接+1,复制粘贴-1^^