Recyclerview预加载

先说下写文章的起因,本人在SmartRefreshLayout刷新群待了有一段时间了,最近群里要转移还是什么的,反正是要加新群,以后加群貌似是要收费入群,然后我们这些潜水党,现在可以免费入群,入群的福利呢,就是可以群主免费解决问题,不说收费入群的问题,说说这个问题怎么出来的呢;进群后先跟群主扯了会,混个脸熟,以后就可以厚脸皮的去问问题了,正好看到他们在说这个Recyclerview预加载的问题;本着多学好问的态度,本人就去百度,然后搜到了一篇不错的文章,附上链接(Ps:应该是个大神,反正比我厉害的都是大神)http://www.cnblogs.com/baiqiantao/p/6958811.html,但是看完懵逼了,好几种解决方案,还被说是不行的方案,有几种我确实也想到了,但是为什么被说成不靠谱的方案呢,然后我就去找了郭霖大神,郭神给我推荐了一种方案说是google采集用的方法;现贴出来,方便以后使用,附上郭神给的链接(Ps:需要翻墙)https://gist.github.com/ssinss/e06f12ef66c51252563e

下面是代码,很简单,翻不了墙的可以看下

// An highlighted block
public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
    public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName();

    public int previousTotal = 0; // The total number of items in the dataset after the last load  总数据
    private boolean loading = true; // True if we are still waiting for the last set of data to load. 是否提前加载
    public int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.
    int firstVisibleItem, visibleItemCount, totalItemCount;
    private int current_page = 1;
    private LinearLayoutManager mLinearLayoutManager;

    public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) {
        this.mLinearLayoutManager = linearLayoutManager;
    }
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        visibleItemCount = recyclerView.getChildCount();
        totalItemCount = mLinearLayoutManager.getItemCount();
        firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();

        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount)
                <= (firstVisibleItem + visibleThreshold)) {
            // End has been reached

            // Do something
            current_page++;

            onLoadMore(current_page);

            loading = true;
        }
    }

    public abstract void onLoadMore(int current_page);
    public void reset(int previousTotal, boolean loading) {
        this.previousTotal = previousTotal;
        this.loading = loading;
    }
}
  endlessScrollListener=new EndlessRecyclerOnScrollListener(manager) {
            @Override
            public void onLoadMore(int current_page) {
                
                init(current_page);
            }
        };
  recycler_id.addOnScrollListener(endlessScrollListener);
发布了1 篇原创文章 · 获赞 4 · 访问量 2941
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览