RecycleView 滑动到底部后,向下滑动连带BottomShee一起滑动
时间: 2023-05-28 09:04:54 浏览: 88
要实现RecycleView滑动到底部后,向下滑动连带BottomSheet一起滑动,可以使用RecyclerView的addOnScrollListener监听RecycleView的滑动,当滑动到底部时,再使用BottomSheetBehavior将BottomSheet一起滑动。
具体实现步骤如下:
1. 获取BottomSheetBehavior对象:
```java
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
```
2. 在RecyclerView中添加滑动监听器:
```java
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int lastVisiblePosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition();
int totalCount = recyclerView.getLayoutManager().getItemCount();
if (lastVisiblePosition == totalCount - 1 && dy > 0) {
// 滑动到底部,向下滑动BottomSheet
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
});
```
3. 在BottomSheet中添加状态监听器,并在状态为STATE_EXPANDED时禁止RecyclerView的滑动:
```java
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
// 禁止RecyclerView的滑动
recyclerView.stopScroll();
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
```
这样,当RecyclerView滑动到底部时,向下滑动BottomSheet就会连带滑动了。
阅读全文