【问题标题】:Is it possible to add a ScrollView to a AppBarLayout是否可以将 ScrollView 添加到 AppBarLayout
【发布时间】:2016-08-16 06:22:52
【问题描述】:

我需要与两个ScrollViews 合作。一个在AppBarLayout 里面,另一个在外面。

对于外部ScrollView,我使用NestedScrollViewappbar_scrolling_view_behavior,它工作正常。

对于内部,我使用Scrollviewapp:layout_scrollFlags="scroll|enterAlways|snap"

我的问题是NestedScrollView 似乎覆盖了ScrollView 事件,即使我触摸ScrollView 区域,NestedScrollView 也是滚动的那个。

有什么办法可以做到吗?

见下面的代码:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"    
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@android:color/white"
            app:layout_scrollFlags="scroll|enterAlways|snap">

            <TextView                
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>  

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

     <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView                
               android:layout_width="match_parent"
               android:layout_height="wrap_content"/>   

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

【问题讨论】:

    标签: android android-scrollview android-coordinatorlayout android-nestedscrollview


    【解决方案1】:

    也许这会起作用:

     <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"    
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@android:color/white"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            android:id="@+id/scr">
    
            <TextView                
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>  
    
        </ScrollView>       
     </android.support.design.widget.AppBarLayout>
    
     <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
        <TextView                
               android:layout_width="match_parent"
               android:layout_height="wrap_content"/>   
    
    </android.support.v4.widget.NestedScrollView>
    

    ScrollView scr = (ScrollView) findViewById(R.id.scr);
    scr.setOnTouchListener(new ScrollView.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                // Disallow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(true);
                break;
    
            case MotionEvent.ACTION_UP:
                // Allow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }
    
            // Handle ScrollView touch events.
            v.onTouchEvent(event);
            return true;
        }
    });
    

    它将覆盖滚动视图中的滚动方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-24
      • 2017-11-02
      • 2014-08-16
      • 1970-01-01
      • 2016-09-17
      相关资源
      最近更新 更多