Alex Fragotsis
Alex Fragotsis

Reputation: 1268

Android collapsing toolbar, collapse only when user touches recycler view

I have the following layout

[CoordinatorLayout]
 [AppBarLayout]
  [CollapsingToolbarLayout]
   [Toolbar]
    [RelativeLayout/]
   [/Toolbar]
  [/CollapsingToolbarLayout]
 [AppBarLayout]
 [RecyclerView/]
[/CoordinatorLayout]

The collapsing toolbar works fine.

My recycle view is like this

<android.support.v7.widget.RecyclerView
    android:id="@+id/profile_grid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:overScrollMode="never"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

When there are not enough items you can't scroll and collapse the toolbar.

If you try to touch the appbar and scroll upwards it collapses.

Do you know a way to stop that and make it collapse ONLY when the user touches the recycler?

Upvotes: 0

Views: 774

Answers (1)

Alex Fragotsis
Alex Fragotsis

Reputation: 1268

The answer was simple as I saw here

if (appBarLayout.getLayoutParams() != null) {
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
AppBarLayout.Behavior appBarLayoutBehaviour = new AppBarLayout.Behavior();
appBarLayoutBehaviour.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
    @Override
    public boolean canDrag(@NonNull AppBarLayout appBarLayout) {
        return false;
    }
});
layoutParams.setBehavior(appBarLayoutBehaviour);

}

Upvotes: 1

Related Questions