Android 判断Recycleview滑动到顶部还是底部

方法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 onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                //根据自己的应用场景,也可以在这里调用
            }
            
            @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, "滑动到底部");
            }
        });

方法3:

recycleview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int topRowVerticalPosition = (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 :
                        recyclerView.getChildAt(0).getTop();
               if( topRowVerticalPosition >= 0){
               //滑动到顶部
               }
            }
        });

————————————————
版权声明:本文为CSDN博主「lylodlig」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lylodyf/article/details/80950000

可以通过监听RecyclerView滑动事件来控制BottomSheet的状态。具体实现步骤如下: 1. 获取RecyclerView对象,设置滑动监听。 ```kotlin val recyclerView: RecyclerView = findViewById(R.id.recyclerView) recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { super.onScrolled(recyclerView, dx, dy) // 滑动事件处理 } }) ``` 2. 判断RecyclerView是否滑动顶部。 ```kotlin val isTop = !recyclerView.canScrollVertically(-1) ``` 3. 获取BottomSheetBehavior对象,设置状态。 ```kotlin val bottomSheetLayout: LinearLayout = findViewById(R.id.bottomSheetLayout) val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetLayout) if (isTop) { bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED } else { bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED } ``` 完整示例代码如下: ```kotlin val recyclerView: RecyclerView = findViewById(R.id.recyclerView) recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { super.onScrolled(recyclerView, dx, dy) val isTop = !recyclerView.canScrollVertically(-1) val bottomSheetLayout: LinearLayout = findViewById(R.id.bottomSheetLayout) val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetLayout) if (isTop) { bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED } else { bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED } } }) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值