0

I am trying to achieve something with collapsible toolbar layout and I have 2 tabs with a viewPager2

Below are two images for example for the case of expanded screenshot and collapsed screenshot.

Below is the code I have in XML not sure where it is going wrong... my scroll is not working proper in this case. I want the toolbar to be stick on top when collapsed

I tried adding toolbar inside AppbarLayout but that didn't worked.

Also tried below links somehow not working for me Link1 Link2

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white_dark_blue">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white_cream_darkest_blue"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:windowSoftInputMode="adjustResize">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white_cream_darkest_blue"
                app:collapsedTitleTextAppearance="@color/text_color"
                app:contentScrim="@color/white_cream_darkest_blue"
                app:expandedTitleTextAppearance="@color/text_color"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:statusBarScrim="@color/white_cream_darkest_blue">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/margin_start"
                    android:layout_marginTop="40dp"
                    android:layout_marginEnd="@dimen/margin_end"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/textViewTitle"
                        style="@style/screen_title"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="30dp"
                        android:text="Mailing\nAddress"
                        android:textColor="@color/text_color" />

                    <TextView

                        android:id="@+id/textViewSubTitle"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="10dp"
                        android:fontFamily="@font/roboto_light"
                        android:text="We'll send your bill to this address."
                        android:textAlignment="center"
                        android:textColor="@color/text_color"
                        android:textSize="20sp"
                        android:visibility="gone" />

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="30dp" />
                </LinearLayout>

                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="80dp"
                    android:layout_marginStart="@dimen/margin_start"
                    android:layout_marginEnd="@dimen/margin_end"
                    android:windowSoftInputMode="adjustResize"
                    app:contentInsetStartWithNavigation="0dp"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/text_service"
                    app:title="">

                    <TextView
                        android:id="@+id/menuOption"
                        style="@style/screen_sub_title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/roboto_medium"
                        android:text="Mailing Address"
                        android:textColor="@color/text_color"
                        android:textSize="20sp"
                        android:visibility="gone" />


                </androidx.appcompat.widget.Toolbar>

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

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

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:clipToPadding="false"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tabLayoutMailingAddress"
                    style="@style/tabLayoutStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/margin_start"
                    android:layout_marginEnd="@dimen/margin_end"
                    app:layout_constraintTop_toTopOf="parent"
                    app:tabInlineLabel="true"
                    app:tabTextAppearance="@style/MyCustomTextAppearance" />

                <androidx.viewpager2.widget.ViewPager2
                    android:id="@+id/viewPagerMailingAddress"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    app:layout_constraintHeight_percent="1.2"
                    app:layout_constraintTop_toBottomOf="@+id/tabLayoutMailingAddress" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.core.widget.NestedScrollView>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    
</layout>

Darpal
  • 352
  • 5
  • 20

1 Answers1

0

ViewPager2 won't work well inside NestedScrollView as its inner RecyclerView enables the nested scrolling by default.

To fix the misbehavior, you need to disable this nested scrolling of the ViewPager2 RecyclerView programmatically as it's not accessible in layout:

Kotlin:

viewPager.children.find { it is RecyclerView }?.let {
    (it as RecyclerView).isNestedScrollingEnabled = false
}

Java:

for (int i = 0; i < viewPager.getChildCount(); i++) {
    View child = viewPager.getChildAt(i);
    if (child instanceof RecyclerView) {
        ((RecyclerView) child).setNestedScrollingEnabled(false);
        break;
    }
}
Zain
  • 37,492
  • 7
  • 60
  • 84
  • So the reason why I am using ```ViewPager2``` is I have a couple of ```EditText``` which I need for user input. – Darpal Mar 16 '22 at 19:34
  • Are those of the `TabLayout` tabs? – Zain Mar 16 '22 at 19:36
  • I have 2 Tabs ... each tabs (has a fragment) and has multiple editTexts inside both fragments (you can check in screenshots attached in the question) Collapsed and Expanded ones – Darpal Mar 16 '22 at 19:37
  • So, those fragments are the pages of the ViewPager; scrolling them to have the collapsing effect still shows a problem? – Zain Mar 16 '22 at 19:39
  • Umm kinda yes ... your code somewhat resolved my issue but it just stops scrolling as soon as the toolbar is collapsed at the top... then I am not able to scroll more – Darpal Mar 16 '22 at 19:41
  • 1
    Nevermind Zain I figured out my issue .... I was able to resolve it in ```ViewPager``` I had ```android:layout_height="0dp" app:layout_constraintHeight_percent="1.2"``` which was creating issue now it seems to be working Thank you for your help!! – Darpal Mar 16 '22 at 19:44
  • 1
    Good job.. I guess you could constraint the viewpager to the bottom – Zain Mar 16 '22 at 19:47
  • One last thing If you know where I can understand more on Why ```ViewPager2``` not good with ```NestedScrollView```. Any article or google issue you came across? – Darpal Mar 16 '22 at 19:51
  • I came across this issue; it's not something special to the `NestedScrollView`; sorry if I didn't express it well; I meant when VP2 wrapped in another scrolling view in general; I had something similar to that; let me check if there is something useful online – Zain Mar 16 '22 at 19:55
  • No worries! just curious to know more about it. Appreciate it! – Darpal Mar 16 '22 at 19:57
  • 1
    See [this issue tracker](https://issuetracker.google.com/issues/123006042) pls; it is a mix of this issue; and another issue of the nested scrolling views of ViewPager2; it also discussed by documentation [here](https://developer.android.com/training/animation/vp2-migration#nested-scrollables) – Zain Mar 16 '22 at 20:22