Scrollview не прокручивается на нижнем листе Android

Я попробовал новый BottomSheet в поддержке Android. Внутри BottomSheet я поместил TextView и ScrollView. BottomSheet показывает просто отлично, единственная проблема, которую я обнаружил, заключается в том, что ScrollView в BottomSheet не прокручивается. Каждый раз, когда я пытаюсь прокрутить, либо макет в основном действии прокручивается, либо BottomSheet изменяется состояние от свертывания до расширения.

Это фрагмент кода моего класса активности:

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>

person asiansaga    schedule 11.04.2016    source источник
comment
Пожалуйста, опубликуйте свой код для макета, чтобы мы могли проверить. Также, чтобы ScrollView работал, под ним должен быть только один дочерний элемент.   -  person Saumik Bhattacharya    schedule 11.04.2016
comment
см. эту ссылку, которая разрешила мою проблему.   -  person abbasalim    schedule 08.05.2018
comment
вместо этого используйте NestedScrollView.   -  person Gentle    schedule 23.06.2018
comment
NestedScrollView без каких-либо конкретных флагов решает такие проблемы даже с BottomSheetDialogFragment.   -  person Saad Bilal    schedule 05.01.2019


Ответы (6)


Надеюсь, вы уже поняли это, но измените View bottomSheet на NestedScrollView bottomSheet.

person Chris Nguyen    schedule 16.01.2017
comment
Я заменил root ‹ScrollView ..› на ‹androidx.core.widget.NestedScrollView..›, и это работает, спасибо! - person AlexS; 06.05.2020
comment
Это должно быть помечено как принятый ответ! - person Samuel T. Chou; 31.05.2021

Я решил проблему, выполнив следующие действия:

  • Во-первых: не используйте ScrollView при использовании CoordinatorLayout вместо пользовательского NestedScrollView, он работает намного лучше с CoordinatorLayout.

  • Второе: поместите пустое представление с android:layout_height внизу, но внутри вашего NestedScrollView, например:

    <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" />
    

person VaiVirus    schedule 24.02.2017
comment
Как это работает? Я получаю java.lang.IllegalStateException: ScrollView может содержать только один прямой дочерний элемент - person Ruben2112; 01.08.2017
comment
этот ответ вообще не имеет смысла - person GaRRaPeTa; 28.02.2019
comment
это вообще не работает и заканчивается исключением, как сказал Рубен - person Manti_Core; 06.06.2020

Дополнение к ответу «джобберта»:

Если вы всегда возвращаете «false», может случиться так, что нижний лист вообще не работает. Это случилось со мной, когда я также использую просмотрщик внутри макета координатора нижних листов. Чтобы на самом деле это исправить, нужно проверить, было ли касание внутри вложенного прокрутки или нет. Это можно легко вычислить и привести к наиболее общему решению:

 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)

}
person 8K Creative Commons    schedule 02.07.2019

Используйте ниже настраиваемое поведение нижнего листа

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;
    }
}
person Aghil C M    schedule 03.11.2016

это сработало для меня, попробуйте сначала использовать 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>
person Harshit Maheshwari    schedule 20.03.2019

Ответ @Aghil C M сработал, но перевел его на Котлин, так как автоматический перевод не помог.

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
    }
}
person jobbert    schedule 02.08.2018
comment
Должно быть, это был неверный клик, если вы отредактируете, я могу удалить это! - person sixones; 31.03.2019
comment
На самом деле это приведет к новым проблемам. Если вы всегда возвращаете false, нижний лист не будет работать, когда в макете координатора есть несколько дочерних элементов. Смотрите мой ответ... - person 8K Creative Commons; 02.07.2019
comment
@8KCreativeCommons Нет времени проверять это, но кажется законным. - person jobbert; 10.07.2019