1. 程式人生 > >判斷RecyclerView滑動到底部和頂部

判斷RecyclerView滑動到底部和頂部

方法1

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Log.i(TAG, "--------------------------------------"
); if(mRecyclerView.canScrollVertically(1)){ Log.i(TAG, "direction 1: true"); }else { Log.i(TAG, "direction 1: false");//滑動到底部 } if(mRecyclerView.canScrollVertically(-1)){ Log.i(TAG
, "direction -1: true"); }else { Log.i(TAG, "direction -1: false");//滑動到頂部 } } });

方法2

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.
onScrolled(recyclerView, dx, dy); Log.i(TAG, "--------------------------------------"); LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); int firstCompletelyVisibleItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition(); Log.i(TAG, "firstCompletelyVisibleItemPosition: "+firstCompletelyVisibleItemPosition); if(firstCompletelyVisibleItemPosition==0) Log.i(TAG, "滑動到頂部"); int lastCompletelyVisibleItemPosition = layoutManager.findLastCompletelyVisibleItemPosition(); Log.i(TAG, "lastCompletelyVisibleItemPosition: "+lastCompletelyVisibleItemPosition); if(lastCompletelyVisibleItemPosition==layoutManager.getItemCount()-1) Log.i(TAG, "滑動到底部"); } });