4

I want to disable scrolling by touch behaviour for the collapsing toolbar. It should only be collapsing, if triggered by the RecyclerView (which is working). I thought why not just disable focus... android:focusableInTouchMode="false" but it is not working. I could change the layout_scrollFlags but then... My question therefore, is there a simple solution for this?

XML:

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/mCollapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="@dimen/header_size"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                ...
                app:layout_collapseMode="parallax"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/mToolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin" />

        </android.support.design.widget.CollapsingToolbarLayout>

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

    <android.support.v7.widget.RecyclerView
        ...
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</android.support.design.widget.CoordinatorLayout>
Javatar
  • 1,913
  • 22
  • 32

1 Answers1

15

Found a working solution here: https://stackoverflow.com/a/35465719/2033223. So now it is only scrollable, if there are enough elements in the recyclerView.

XML:

<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/mCollapsingToolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="@dimen/header_size"
    android:fitsSystemWindows="true"
    app:scrimAnimationDuration="300"
    app:contentScrim="?attr/colorPrimary">

Activity:

public class MainActivity extends AppCompatActivity {

    private CollapsingToolbarLayout collapsingToolbarLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        collapsingToolbarLayout = 
              (CollapsingToolbarLayout) findViewById(R.id.mCollapsingToolbar);

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (rv.canScrollVertically(DOWN) || rv.canScrollVertically(UP)) {
                    controller.enableScroll();
                } else {
                    controller.disableScroll();
                }
            }
        }, 100);
    }

    private void enableScroll() {
        final AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams)
                                  collapsingToolbarLayout.getLayoutParams();
        params.setScrollFlags(
                AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL 
                | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED
        );
        collapsingToolbarLayout.setLayoutParams(params);
    }

    private void disableScroll() {
        final AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams)
                                  collapsingToolbarLayout.getLayoutParams();
        params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL);
        collapsingToolbarLayout.setLayoutParams(params);
    }
}

PS: findLastCompletelyVisibleItemPosition() wasn't enough for my case. But I use it too. (check out link below) https://stackoverflow.com/a/31460285/2033223

EDIT: If you want to make sure, that the correct scroll status is set, use notifications/events (not timed update), after the list is initialized (view is updated). Otherwise it can set the scroll flags incorrectly.

Javatar
  • 1,913
  • 22
  • 32
  • 1
    instead `params.setScrollFlags(0); ` to `params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL); ` readablility better than before – 최봉재 Feb 27 '20 at 04:40