Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android CoordinatorLayout + AppbarLayout + Viewpager always scrolling

I have a classic layout with a ToolBar on the top, a TabLayout below it, and a ViewPager switching tabs from the TabLayout. When content in the ViewPager is scrollable, the ToolBar should scroll out of sight, and the TabLayout should follow and stick when it reaches the top.

All this is good in my current code, except, the ToolBar is always scrollable, regardless of the size of the ViewPager's content. See my code below. Any brilliant ideas on how to fix this?

<?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"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">      <android.support.design.widget.AppBarLayout         android:id="@+id/app_bar_layout"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:background="@color/primary"         android:orientation="vertical">          <android.support.v7.widget.Toolbar             android:id="@+id/toolbar"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:theme="@style/AppTheme.ToolBar"             app:layout_scrollFlags="scroll|enterAlways" />          <android.support.design.widget.TabLayout             android:id="@+id/tabs"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:background="?attr/colorPrimary"             android:scrollbars="horizontal"             app:tabIndicatorColor="@color/black_text" />      </android.support.design.widget.AppBarLayout>      <android.support.v4.view.ViewPager         android:id="@+id/tabs_activity_view_pager"         android:layout_width="match_parent"         android:layout_height="wrap_content"         app:layout_behavior="@string/appbar_scrolling_view_behavior" />  </android.support.design.widget.CoordinatorLayout> 

EDIT:

I can see that the viewPager's height is the same as the height for the entire root view. This might be intentded, as the appbar_scrolling_view_behavior does seem to add a top and bottom offset. It does however seem weird, since it will result in always scrolling the toolbar and tabbar.

like image 457
Kenneth Avatar asked Sep 18 '15 09:09

Kenneth


People also ask

How do I disable scrolling in collapsing ToolBar layout Android?

The solution is simple, we just need to set the app:scrimAnimationDuration=”0" in our collapsing toolbar layout like the below code snippet. Now just run the code and see the results, you will see then there will be no fading animation anymore.

How do you set layout behavior programmatically?

You can set the behavior on an instance of CoordinatorLayout. LayoutParams with setBehavior method. To get a proper Behavior object that represents the same thing as @string/appbar_scrolling_view_behavior you should create an instance of AppBarLayout. ScrollingViewBehavior .

What is the difference between AppBarLayout and ToolBar?

AppBarLayout is a parent layout of ToolBar and ToolBar is custom ActionBar. if you want scroll action on the ToolBar so you should write ToolBar into the AppBarLayout,before you will write code for scroll the ToolBar, you must know the NestedScrollBar,it is used to scroll the ToolBar.

Why does collapsingtoolbarlayout stop reacting to scroll when viewpager2 changes page?

Sign in to your account CollapsingToolbarLayout stops reacting to scroll when ViewPager2 with FragmentStateAdapter changes page (occurs with either ViewPager or ViewPager2). Happens if ViewPager's Fragment content has either NestedScrollView or RecyclerView inside.

What is use of coordinatorlayout in Android?

What is use in Android? An enclosing CoordinatorLayout can be used to achieve Material Design Scrolling Effects when using inner layouts that support Nested Scrolling, such as NestedScrollView or RecyclerView. app:layout_scrollFlags="scroll|enterAlways" is used in the Toolbar properties

How to automatically scroll any appbarlayout siblings in Android?

Behavior which should be used by View s which can scroll vertically and support nested scrolling to automatically scroll any AppBarLayout siblings. Returns the distance that this view should overlap any AppBarLayout . Set the distance that this view should overlap any AppBarLayout .

How to notify the appbarlayout when a scroll event occurs?

The support library contains a special string resource @string/appbar_scrolling_view_behavior that maps to AppBarLayout.ScrollingViewBehavior, which is used to notify the AppBarLayout when scroll events occur on this particular view. The behavior must be established on the view that triggers the event.


2 Answers

I have solved the issue, tried over the example Google template and find out that

app:layout_behavior="@string/appbar_scrolling_view_behavior"  

line must be added into view pager properties xml. It solved my problem.

like image 97
Mert Avatar answered Oct 04 '22 02:10

Mert


I suggested you try this sample.

this is a layout like your layout in the sample.

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:id="@+id/main_content"     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">          <android.support.v7.widget.Toolbar             android:id="@+id/toolbar"             android:layout_width="match_parent"             android:layout_height="?attr/actionBarSize"             android:background="?attr/colorPrimary"             app:popupTheme="@style/ThemeOverlay.AppCompat.Light"             app:layout_scrollFlags="scroll|enterAlways" />          <android.support.design.widget.TabLayout             android:id="@+id/tabs"             android:layout_width="match_parent"             android:layout_height="wrap_content" />      </android.support.design.widget.AppBarLayout>      <android.support.v4.view.ViewPager         android:id="@+id/viewpager"         android:layout_width="match_parent"         android:layout_height="match_parent"         app:layout_behavior="@string/appbar_scrolling_view_behavior" />      <android.support.design.widget.FloatingActionButton         android:id="@+id/fab"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="end|bottom"         android:layout_margin="@dimen/fab_margin"         android:src="@drawable/ic_done" />  </android.support.design.widget.CoordinatorLayout> 
like image 45
Mohammad Hossein Gerami Avatar answered Oct 04 '22 02:10

Mohammad Hossein Gerami