Android RecyclerView分页监听底部状态源码

我在这里直接封装成了一个类,直接上代码:

/**
 * Recycler分页
 *
 * @author https://blog.csdn.net/bobxie520
 *         Created by haibo on 2016/7/28.
 */
public class RecyclerPageView extends RecyclerView {
    public RecyclerPageView(Context context) {
        super(context);
    }
    public RecyclerPageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    public RecyclerPageView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.addOnScrollListener(new MyOnScrollListener());
    }
    private class MyOnScrollListener extends RecyclerView.OnScrollListener {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            if (newState == SCROLL_STATE_IDLE) {
                LayoutManager layoutManager = getLayoutManager();
                if (getLayoutManager() instanceof StaggeredGridLayoutManager) {
                    StaggeredGridLayoutManager layout = (StaggeredGridLayoutManager) layoutManager;
                    // 获取瀑布流有多少列
                    int column = layout.getColumnCountForAccessibility(null, null);
                    int positions[] = new int[column];
                    // 获取lastItem的positions
                    layout.findLastVisibleItemPositions(positions);
                    for (int i = 0; i < positions.length; i++) {
                        /**
                         * 判断lastItem的底边到recyclerView顶部的距离
                         * 是否小于recyclerView的高度
                         * 如果小于或等于 说明滚动到了底部
                         */
                        if (positions[i] >= (layout.getItemCount() - column)
                                && layout.findViewByPosition(positions[i]).getBottom() <= getHeight()) {
                            Log.d("admin", "到底部了");
                            break;
                        }
                    }
                } else if (getLayoutManager() instanceof LinearLayoutManager) {
                    LinearLayoutManager layout = (LinearLayoutManager) layoutManager;
                    int position = layout.findLastVisibleItemPosition();
                    if (position - 1 == layout.getItemCount()
                            && layout.findViewByPosition(position).getBottom() <= getHeight()) {
                        Log.d("admin", "到底部了");
                    }
                }
            }
            super.onScrollStateChanged(recyclerView, newState);
        }
    }
}

如果感觉帮助到你了,赶快给个赞吧。哈哈

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> <link type="text/css" rel="stylesheet" href="css/jquery-ui-1.9.2.custom.min.css"> <link type="text/css" rel="stylesheet" href="css/ui.jqgrid.css"> <link rel="stylesheet" type="text/css" media="screen" href="css/ui.multiselect.css"> <script type="text/javascript" src="js/jquery-1.11.0.min.js" ></script> <script type="text/javascript" src="js/jquery-ui-1.9.2.custom.min.js"></script> <script type="text/javascript" src="js/jquery.layout-1.2.0.js"></script> <script type="text/javascript" src="js/i18n/grid.locale-cn.js"></script> <script type="text/javascript" src="js/jquery.jqGrid.min.js"></script> <script type="text/javascript" src="js/jquery.tablednd.js"></script> <script type="text/javascript" src="js/jquery.contextmenu.js"></script> <script type="text/javascript" src="js/ui.multiselect.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#list").jqGrid({ url:"happyEvent/jsonlist", datatype: "json", height: "auto", width:"auto", colNames:['id','时间', '用户名', '标题','内容'], colModel:[ {name:'id',index:'id', width:60, sorttype:"int"}, {name:'time',index:'time', width:90, sorttype:"date"}, {name:'name',index:'name', width:100}, {name:'title',index:'title', width:300, align:"left",sorttype:"float"}, {name:'content',index:'content', width:80, align:"left",sorttype:"float"} ], rowNum:2, rowList:[2,4,6], sortname: 'id', pager:"#pager", multiselect: true, caption: "喜事列表" }); $("#list").jqGrid('navGrid','#pager',{edit:false,add:false,del:false}); }); </script> </head> <body> <table id="list"></table> <div id="pager"></div> </body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泡在网上的蜘蛛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值