天天看點

監聽recyclerview滑動到底部(橫向,縱向)

監聽橫向Recyclerview滑動到底部

recyclerview.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {

            }

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {

                //得到目前顯示的最後一個item的view
                val lastChildView = recyclerView.layoutManager!!.getChildAt(recyclerView.layoutManager!!.childCount - 1)
                //得到lastChildView的bottom坐标值
                val lastChildBottom = lastChildView!!.right
                //得到Recyclerview的底部坐标減去底部padding值,也就是顯示内容最底部的坐标
                val recyclerBottom = recyclerView.right - recyclerView.rightPadding
                //通過這個lastChildView得到這個view目前的position值
                val lastPosition = recyclerView.layoutManager!!.getPosition(lastChildView)

                //判斷lastChildView的bottom值跟recyclerBottom相等
                //判斷lastPosition是不是最後一個position
                if (lastChildBottom == recyclerBottom && lastPosition == recyclerView.layoutManager!!.itemCount - 1) {
                    Toast.makeText(context, "到底了", Toast.LENGTH_SHORT).show();
                }
            }
        })
           

監聽縱向Recyclerview滑動到底部

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

        //得到目前顯示的最後一個item的view
        View lastChildView = recyclerView.getLayoutManager().getChildAt(recyclerView.getLayoutManager().getChildCount()-1);
        //得到lastChildView的bottom坐标值
        int lastChildBottom = lastChildView.getBottom();
        //得到Recyclerview的底部坐标減去底部padding值,也就是顯示内容最底部的坐标
        int recyclerBottom =  recyclerView.getBottom()-recyclerView.getPaddingBottom();
        //通過這個lastChildView得到這個view目前的position值
        int lastPosition  = recyclerView.getLayoutManager().getPosition(lastChildView);

        //判斷lastChildView的bottom值跟recyclerBottom是否相等
        //判斷lastPosition是不是最後一個position
        if(lastChildBottom == recyclerBottom && lastPosition == recyclerView.getLayoutManager().getItemCount()-1 ){
            Toast.makeText(context, "到底了", Toast.LENGTH_SHORT).show();
        }
    }
    });