【问题标题】:Scrollview not scrolling in Android bottomsheet滚动视图不在Android底页中滚动
【发布时间】:2016-08-01 12:06:40
【问题描述】:

我在 Android 支持中尝试了新的 BottomSheet。在BottomSheet 里面我放了一个TextView 和一个ScrollViewBottomSheetshowing 很好,我发现的唯一问题是BottomSheet 中的ScrollView 没有滚动。每次我尝试滚动时,滚动的主要活动中的布局或 BottomSheet 将状态从折叠更改为展开。

这是我的活动类代码的 sn-p:

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 工作,它下面应该只有一个孩子。
  • 看到这个link,解决了我的问题。
  • 改用 NestedScrollView。
  • NestedScrollView 没有任何特定标志,即使使用 BottomSheetDialogFragment 也能解决此类问题。

标签: android android-layout bottom-sheet


【解决方案1】:

希望您现在已经弄清楚了,但请将 View bottomSheet 更改为 NestedScrollView bottomSheet

【讨论】:

  • 我将根 替换为 并且可以,谢谢!
  • 这应该被标记为已接受的答案!
【解决方案2】:

我通过以下操作解决了这个问题 -

  • 首先:在使用 CoordinatorLayout 时不要使用 ScrollView,而是使用 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" />
    

【讨论】:

  • 它是如何工作的?我得到一个 java.lang.IllegalStateException: ScrollView can host only one direct child
  • 这个答案毫无意义
  • 这根本不起作用,最终会出现 ruben 所说的异常
【解决方案3】:

除了“jobbert”的答案:

如果您总是返回“false”,则底页可能根本不工作。这发生在我身上,当我还在底页 coordinatorlayout 中使用 viewpager 时。要真正修复它,需要检查触摸是否在嵌套滚动视图内。这可以很容易地计算并得出最通用的解决方案:

 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】:

    使用下面的自定义底页行为

    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;
        }
    }
    

    【讨论】:

      【解决方案5】:

      这对我有用 尝试使用第一个滚动视图,然后使用线性或相对布局 并使用它上面的嵌套滚动视图

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

      【讨论】:

        【解决方案6】:

        @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
            }
        }
        

        【讨论】:

        • 一定是误点击,如果你编辑我可以删除它!
        • 这实际上会导致新的问题。如果您总是返回“false”,那么当 coordinatorlayout 中有多个子项时,底页将不起作用。看我的回答...
        • @8KCreativeCommons 没来得及检查,但似乎合法。
        猜你喜欢
        • 1970-01-01
        • 2011-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多