BottomSheetDialogFragment - 允许滚动子项

pro*_*m85 12 android bottom-sheet

我有BottomSheetDialogFragment一个RecyclerView.问题是,我想禁用拖动关闭功能,BottomSheetDialogFragment只要RecyclerView不向上滚动(目前我无法滚动我RecyclerView的尝试将始终关闭BottomSheetDialogFragment).任何想法如何实现这一目标?

Bro*_*Han 11

可以通过这种方式解决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>
Run Code Online (Sandbox Code Playgroud)

  • 这是一个糟糕的做法。分页调用被无限调用。 (2认同)

小智 7

Fragment在你的扩展RecyclerView中尝试这个onCreateView

recyclerView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.getParent().requestDisallowInterceptTouchEvent(true);
        v.onTouchEvent(event);
        return true;
    }
});
Run Code Online (Sandbox Code Playgroud)


A. *_*rov 6

只需向您的布局添加一个 (!) 属性:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/receiversList"
    android:nestedScrollingEnabled="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
Run Code Online (Sandbox Code Playgroud)

android:nestedScrollingEnabled="false"做所有的工作。另外,如果你环绕你的RecyclerViewwith SwipeRefreshLayout- 刷新功能将继续工作。即使您将布局置于更复杂的视图中,您也BottomSheetDialog将继续支持向下拖动以关闭行为(如果在RecyclerView&之外发生向下滑动手势的触摸SwipeRefreshLayout)。


小智 1

更改 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) {

                }
            });
        }
Run Code Online (Sandbox Code Playgroud)

  • 这将完全禁用关闭拖动功能,但我可以检查“RecyclerView”是否也滚动到顶部,那么这应该可以工作 (2认同)