Scrollview无法在Android底片中滚动

asi*_*aga 24 android android-layout bottom-sheet

BottomSheet在android支持中尝试了新功能.在里面BottomSheet我放了一个TextView和一个ScrollView.该BottomSheet表现得很好,我发现的唯一的问题是,ScrollViewBottomSheet没有滚动.每次我尝试滚动时,滚动的主要活动中的布局或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();
}
Run Code Online (Sandbox Code Playgroud)

这是我的布局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>
Run Code Online (Sandbox Code Playgroud)

请帮忙.

谢谢

小智 26

希望你现在想出来,但View bottomSheet改为NestedScrollView bottomSheet.

  • 我将根 &lt;ScrollView ..&gt; 替换为 &lt;androidx.core.widget.NestedScrollView..&gt; 这有效,谢谢! (13认同)
  • 这应该标记为已接受的答案! (2认同)

小智 5

对“jobbert”的答案的补充:

如果您总是返回“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)

}
Run Code Online (Sandbox Code Playgroud)


小智 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;
    }
}
Run Code Online (Sandbox Code Playgroud)