Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Androidx ViewPager not working with scrolling view behavior

So, I'm updating my project from using the Android Support Libraries to using AndroidX. I followed the mirgration documentation and changed each support library for its alternative on AndroidX.

Everything is working perfect, except one thing, I have a screen that is basically an AppBarLayout, that contains a Toolbar and a TabLayout, below I have a ViewPager which contains two Fragments, one for each Tab, the Fragments internally are just displaying a list of items. The idea is that if I scroll down on any of the two Tab's Fragments the Toolbar should collapse. This was working perfectly just before I updated to AndroidX.

Any ideas about how to fix this and keep using the AndroidX library? I really want to stick with the AndroidX library, but I cannot use it, if it's not working.

Here's my before and after of my gradle/xml files:

BEFORE ANDROIDX XML:

<?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:id="@+id/fragmentPetsTabMainContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/fragmentPetsTabAppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/fragmentPetsToolbar"
            app:title="@string/menu_transactions"
            app:titleTextColor="@color/white"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:contentInsetStartWithNavigation="0dp"
            app:layout_scrollFlags="scroll|enterAlways"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/fragmentPetsTabLayout"
            style="@style/CustomTabLayout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="left"
            android:background="?attr/colorPrimary"
            app:tabGravity="fill"
            app:tabMode="fixed">

            <android.support.design.widget.TabItem
                android:id="@+id/fragmentPetsTabOpen"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/fragment_transaction_open_tab"/>

            <android.support.design.widget.TabItem
                android:id="@+id/fragmentPetsTabClosed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/fragment_transaction_closed_tab"/>

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

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

    <android.support.v4.view.ViewPager
        android:id="@+id/fragmentPetsTabViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

AFTER ANDROIDX XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fragmentPetsTabMainContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/fragmentPetsTabAppBar"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/fragmentPetsToolbar"
            app:title="@string/menu_transactions"
            app:titleTextColor="@color/white"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:contentInsetStartWithNavigation="0dp"
            app:layout_scrollFlags="scroll|enterAlways"/>

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/fragmentPestTabLayout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="left"
            android:background="?attr/colorPrimary"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_scrollFlags="enterAlways"
            app:tabGravity="fill"
            app:tabMode="fixed">

            <com.google.android.material.tabs.TabItem
                android:id="@+id/fragmentPetsTabOpen"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/fragment_transaction_open_tab"/>

            <com.google.android.material.tabs.TabItem
                android:id="@+id/fragmentPetsTabClosed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/fragment_transaction_closed_tab"/>

        </com.google.android.material.tabs.TabLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/fragmentPetsTabViewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</androidx.constraintlayout.widget.ConstraintLayout>

BEFORE ANDROIDX DEPENDENCIES:

...
//region Support dependencies
supportRecyclerView :  "com.android.support:recyclerview-v7:$versions.googleSupportVersion",
supportAppCompatV7 : "com.android.support:appcompat-v7:$versions.googleSupportVersion",
supportV13 : "com.android.support:support-v13:$versions.googleSupportVersion",
supportDesign : "com.android.support:design:$versions.googleSupportVersion",
supportCardViewV7 : "com.android.support:cardview-v7:$versions.googleSupportVersion",
supportContraintLayout : 'com.android.support.constraint:constraint-layout:1.1.1',
//endregion
...

....
    implementation libraries.kotlin, libraries.supportAppCompatV7, libraries.glide,
        libraries.daggerAndroidSupport, libraries.supportRecyclerView, libraries.supportV13,
        libraries.supportDesign, libraries.supportCardViewV7, libraries.supportContraintLayout,
        libraries.rxJava2, libraries.rxJava2Kotlin, libraries.rxJavaAndroid,
        libraries.supportAppCompatV7
...

AFTER ANDROIDX DEPENDENCIES

...
        androidXRecyclerView : "androidx.recyclerview:recyclerview:$versions.androidXVersion",
        androidXAppCompat : "androidx.appcompat:appcompat:$versions.androidXVersion",
        androidXV4 : "androidx.legacy:legacy-support-v4:$versions.androidXVersion",
        androidXV13 : "androidx.legacy:legacy-support-v13:$versions.androidXVersion",
        androidXDesign : "com.google.android.material:material:$versions.androidXVersion",
        androidXCardView : "androidx.cardview:cardview:$versions.androidXVersion",
        androidXConstraintLayout : "androidx.constraintlayout:constraintlayout:1.1.2",
...
implementation libraries.androidXAppCompat, libraries.androidXRecyclerView,
        libraries.androidXV13, libraries.androidXDesign, libraries.androidXCardView,
        libraries.androidXConstraintLayout
...

androidXVersion is 1.0.0 and googleSupportVersion is 28.0.0

What I've tried so far:

  • Updating all AndroidX dependencies to the latest version.

  • Changing Activities from AppActivityCompat to just Activity or FragmentActivity.

  • Googled, a lot.

  • I've seen that theres some issues with the scrolling behavior, this one in particular makes me think that this is a bug, but I'm not 100% sure. Already tried the solution there.

  • Also tried this

Any tip is appreciated.

like image 562
4gus71n Avatar asked Oct 26 '18 11:10

4gus71n


1 Answers

You used CoordinatorLayout before AndroidX dependencies which everything were fine since you added:

app:layout_scrollFlags="scroll|enterAlways"

To the Toolbar and after, you used androidx.constraintlayout.widget.ConstraintLayout which is not the same and some behaviors like collapsing or etc might not work with ConstraintLayout.

Use: androidx.coordinatorlayout.widget.CoordinatorLayout instead of androidx.constraintlayout.widget.ConstraintLayout in the root of the layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.appbar.AppBarLayout>
     <androidx.appcompat.widget.Toolbar>
      <com.google.android.material.tabs.TabLayout>
       ...
       ..
       .
like image 139
ʍѳђઽ૯ท Avatar answered Nov 08 '22 10:11

ʍѳђઽ૯ท