Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add a ScrollView to a AppBarLayout

I need to work with two ScrollViews. One inside AppBarLayout and the other outside.

For the outside ScrollView, I'm using a NestedScrollView with the appbar_scrolling_view_behavior and it's working correctly.

For the inside, I'm using an Scrollview with app:layout_scrollFlags="scroll|enterAlways|snap"

My problem is that the NestedScrollView seems to override the ScrollView events, even if I touch the ScrollView area, the NestedScrollView is the one that scrolls.

Is there any way I could do that?

See the code below:

<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>
like image 917
user2494863 Avatar asked Apr 22 '16 14:04

user2494863


1 Answers

Maybe this will work :

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

It will override the scroll method in scroll view.

like image 68
Basu Avatar answered Oct 10 '22 02:10

Basu