我有一个BottomSheetDialogFragment
与RecyclerView
。问题是,我想禁用BottomSheetDialogFragment
的拖动关闭功能,只要RecyclerView
没有向上滚动(目前我无法滚动我的RecyclerView
,因为尝试将始终关闭BottomSheetDialogFragment
)。任何想法如何实现这一目标?
只需将其视为BottomSheetDialog,并在触摸时简单地禁用其拖动或滑动。
创建BottomSheetDialog时,它会自动将您的布局包装在CoordinatorLayout中,因此如果您想从视图中获取行为,请调用
final BottomSheetBehavior behavior = BottomSheetBehavior.from((View)view.getParent());
然后通过这种行为,你可以做你需要的。
final BottomSheetBehavior behavior = BottomSheetBehavior.from((View) view.getParent());
behavior.setHideable(false);
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
这对我有用。希望它有所帮助。
BottomSheetDialog中的RecyclerView滚动问题可以通过这种方式解决。
来自:https://android.jlelse.eu/recyclerview-within-nestedscrollview-scrolling-issue-3180b5ad2542
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
在setupDialog方法中更改BottomSheetDialogFragment中的行为:
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
final CoordinatorLayout.Behavior behavior = layoutParams.getBehavior();
if (behavior != null && behavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) behavior).setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
if (newState == BottomSheetBehavior.STATE_DRAGGING) {
((BottomSheetBehavior) behavior).setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
}
把你的RecyclerView
放在NestedScrollView
下,其中NestedScroll是回收者的父母