在Android底部面板中,ScrollView无法滚动

70

我尝试使用Android支持库中的新 BottomSheet 。在 BottomSheet 内,我放置了一个 TextView 和一个 ScrollView BottomSheet 显示得很好,唯一的问题是 BottomSheet 中的 ScrollView 不能滚动。每次尝试滚动时,要么滚动主活动中的布局,要么 BottomSheet 从折叠状态转换为展开状态。

这是我的Activity类代码片段:

private BottomSheetBehavior behavior;
View bottomSheet;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    setTextViewOnClickListener(this, findViewById(R.id.parentLayout));

    CoordinatorLayout coordinatorLayout = (CoordinatorLayout) findViewById(R.id.main_content);

    // The View with the BottomSheetBehavior
    bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet);
    behavior = BottomSheetBehavior.from(bottomSheet);
    behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            // React to state change

        }


        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            // React to dragging events
        }
    });

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.tv1:
            setTextViewHeader("Header1");
            setTextViewContent("Long_Text_1");

            break;

        case R.id.tv2:
            setTextViewHeader("Header2");
            setTextViewContent("Long_Text_2");

            break;

        case R.id.tv3:
            setTextViewHeader("Header3");
            setTextViewContent("Long_Text_3");

            break;

        default:
            break;
    }

    behavior.setPeekHeight(100);
    behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
    behavior.setHideable(true);

    bottomSheet.requestLayout();
}

这是我的布局XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.rapidgrowsolutions.android.MainActivity">


    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay" />


    </android.support.design.widget.AppBarLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv1"
            style="@style/LightRow"
            android:text="some_long_text_here" />

        <TextView
            android:id="@+id/tv2"
            style="@style/DarkRow"
            android:text="another_long_text_here" />

        <TextView
            android:id="@+id/tv3"
            style="@style/LightRow"
            android:text="another_long_text_here" />
    </LinearLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        app:behavior_hideable="true"
        android:fillViewport="true"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior">


        <android.support.v7.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FF7733"
            android:orientation="vertical">


            <TextView
                android:id="@+id/tvID1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="HEADER"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="9"
                android:background="#ffb773"
                android:fillViewport="true">


                <TextView
                    android:id="@+id/tvID2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#3377ff"
                    android:textAppearance="?android:attr/textAppearanceSmall" />
            </ScrollView>


        </android.support.v7.widget.LinearLayoutCompat>

    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

请发布您的布局代码,以便我们进行检查。此外,为了使ScrollView正常工作,它应该只有一个子元素。 - Saumik Bhattacharya
看这个链接,解决了我的问题。 - abbasalim
请使用NestedScrollView。 - Gentle
NestedScrollView没有任何特定的标志,即使在BottomSheetDialogFragment中也可以解决这些问题。 - Saad Bilal
6个回答

109

希望你现在已经弄清楚了,但是需要将View bottomSheet更改为NestedScrollView bottomSheet


25
我更换了根部的<ScrollView..>为<androidx.core.widget.NestedScrollView..>,这样就可以使用了,谢谢! - AlexS
2
这个答案应该被标记为已接受的答案! - Samuel T. Chou
1
这是正确的答案!请将其标记为已接受。 - juske

5

添加到“jobbert”的答案中:

如果您始终返回“false”,可能会导致底部工作表根本无法工作。当我在底部工作表的协调布局中也使用viewpager时,就会发生这种情况。要实际修复它,需要检查触摸是否在nestedscrollview内。这可以轻松计算并导致最通用的解决方案:

 override fun onInterceptTouchEvent(parent: CoordinatorLayout, child: V, event: MotionEvent): Boolean {

    val nested = child.findViewById<NestedScrollView>(R.id.nested) //NestedScrollView
    var x = event.x
    var y = event.y

    val position = IntArray(2)
    nested.getLocationOnScreen(position)

    var nestedX = position[0]
    var nestedY = position[1]


    var boundLeft = nestedX
    var boundRight = nestedX + nested.width
    var boundTop = nestedY
    var boundBottom = nestedY + nested.height


    if ((x > boundLeft && x < boundRight && y > boundTop && y < boundBottom) || event.action == MotionEvent.ACTION_CANCEL) {
        //Touched inside of the scrollview-> pass the touch event to the scrollview
        return false
    }



    //touched outside, use the parents computation to make the bottomsheet work
    return super.onInterceptTouchEvent(parent, child, event)

}

4
我通过以下方法解决了这个问题-
  • First: don't use ScrollView when you are using CoordinatorLayout instead user NestedScrollView its works much better with CoordinatorLayout.

  • Second: put a blank view with android:layout_height at the bottom, but inside you NestedScrollView eg -

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="vertical">
    
        <ImageView
            android:background="@drawable/username"
            android:id="@+id/userImage_info_search"
            android:layout_gravity="center"
            android:layout_height="100dp"
            android:layout_margin="20dp"
            android:layout_width="100dp" />
    
        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
    
        <View
            android:background="@android:color/black"
            android:layout_height="1dp"
            android:layout_width="match_parent"></View>
    </LinearLayout>
    
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:padding="10dp"
        android:weightSum="3">
    
        <TextView
            style="@style/Bottomsheetstyle"
            android:id="@+id/txtNamelabel_info_search"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:text="Name" />
    
        <TextView
            style="@style/Bottomsheetstyle"
            android:id="@+id/txtName_info_search"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:text="" />
    
    
    </LinearLayout>
    
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
    
        <View
            android:background="@android:color/black"
            android:layout_height="1dp"
            android:layout_width="match_parent"></View>
    </LinearLayout>
    
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:padding="10dp"
        android:weightSum="3">
    
        <TextView
            style="@style/Bottomsheetstyle"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:text="Number" />
    
        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:orientation="horizontal">
    
            <TextView
                style="@style/Bottomsheetstyle"
                android:gravity="center_vertical"
                android:id="@+id/txtNumber_info_search"
                android:layout_gravity="center_vertical"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_weight="1.4"
                android:layout_width="0dp"
                android:text="+XX (XXX) XXX-XXXX" />
    
            <ImageView
                android:background="@drawable/call_save"
                android:id="@+id/call_info_search"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:layout_width="0dp" />
    
            <View
                android:layout_gravity="center"
                android:layout_height="5dp"
                android:layout_width="5dp"></View>
    
            <ImageView
                android:background="@drawable/comment_save"
                android:id="@+id/sms_info_search"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:layout_width="0dp" />
    
    
        </LinearLayout>
    
    
    </LinearLayout>
    
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
    
        <View
            android:background="@android:color/black"
            android:layout_height="1dp"
            android:layout_width="match_parent"></View>
    </LinearLayout>
    
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:padding="10dp"
        android:weightSum="3">
    
        <TextView
            style="@style/Bottomsheetstyle"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:text="Email" />
    
        <TextView
            style="@style/Bottomsheetstyle"
            android:id="@+id/txtEmail_info_search"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:text="" />
    
    
    </LinearLayout>
    
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
    
        <View
            android:background="@android:color/black"
            android:layout_height="1dp"
            android:layout_width="match_parent"></View>
    </LinearLayout>
    
    <View
        android:background="@android:color/transparent"
        android:layout_height="@dimen/somedp"
        android:layout_width="match_parent" />
    


1
这是怎么回事?我收到了一个java.lang.IllegalStateException: ScrollView只能托管一个直接子项的错误。 - Ruben2112
21
这个回答完全没有意义。 - GaRRaPeTa
这根本不起作用,最终会像Ruben所说的那样出现异常。 - MDT

1

这个方法对我很有效 尝试先使用scrollView,然后再使用线性或相对布局 并在它上面还可以使用嵌套scrollView

<RelativeLayout
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    app:behavior_hideable="true"
    app:elevation="4dp"
    android:background="@color/colorPrimaryDark"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    android:clipToPadding="true">


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="16dp"
                android:text="oh hello this is dummy data"
                android:textColor="#FFFFFF"
                android:textSize="20sp" />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@mipmap/ic_launcher" />
            <ImageView
                android:id="@+id/"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@mipmap/ic_launcher" />
            <ImageView
                android:id="@+id/"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@mipmap/ic_launcher" />
            <ImageView
                android:id="@+id/"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@mipmap/ic_launcher" />

            <ImageView
                android:id="@+id/"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@mipmap/ic_launcher" />
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

0

@Aghil C M的答案有效,但是由于自动翻译无法胜任,我将其翻译成了Kotlin。

class CustomBottomSheetBehaviour<V : View> : BottomSheetBehavior<V> {
    constructor() : super()

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    override fun onInterceptTouchEvent(parent: CoordinatorLayout?, child: V, event: MotionEvent?): Boolean {
        return false
    }
}

一定是误点了,如果您编辑的话我可以将其删除! - sixones
这实际上会导致新的问题。如果你总是返回“false”,当协调布局中有多个子元素时,底部工作表将无法正常工作。请参见我的答案... - 8K Creative Commons
@8KCreativeCommons 现在没时间检查,但看起来是合法的。 - jobbert

0
请使用以下自定义底部表单行为:
public class CustomBottomSheetBehaviour<V extends View> extends BottomSheetBehavior {
    public CustomBottomSheetBehaviour() {
        super();
    }

    public CustomBottomSheetBehaviour(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(CoordinatorLayout parent, View child, MotionEvent event) {
        return false;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接