greysqrl
greysqrl

Reputation: 977

Toolbar not scrolling in Coordinator Layout

I have a coordinator layout which contains a ViewPager which in turn contains a RecyclerView.

The issue I am facing is that the toolbar is not resizing as I scroll my RecyclerView.

I have looked at quite a few posts but cannot see what I'm doing wrong.

Any help is very much appreciated.

I have nothing in MainActivity or anywhere else that manipulates the toolbar.

Relevant parts of build.gradle

compileSdkVersion 28
    defaultConfig {
        applicationId "com.elnimijo.apps.blogfeeder"
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
            }
        }

        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha02'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0-alpha02'
    implementation "androidx.fragment:fragment-ktx:1.1.0-alpha04"
    implementation "androidx.coordinatorlayout:coordinatorlayout:1.1.0"
    implementation "androidx.recyclerview:recyclerview:1.1.0"

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools" package="com.mydomain.apps.myapp">

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
            android:name=".app"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:usesCleartextTraffic="true"
            android:theme="@style/AppTheme" tools:ignore="AllowBackup,GoogleAppIndexingWarning">
        <activity
                android:name=".main.ui.MainActivity"
                android:label="@string/app_name"
                android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

Styles

<resources>

   <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorWhite</item>
    </style>

</resources>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                 xmlns:tools="http://schemas.android.com/tools"
                                                 xmlns:app="http://schemas.android.com/apk/res-auto"
                                                 android:id="@+id/main_content"
                                                 android:layout_width="match_parent"
                                                 android:layout_height="match_parent"
                                                 tools:context=".main.ui.MainActivity">

    <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/appbar_padding_top"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                app:title="@string/app_name"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways">
            </androidx.appcompat.widget.Toolbar>

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

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

Fragment

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".rss.ui.RssFeedFragment">

    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recViewRSSFeedAdapter"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="0dp"
            android:paddingBottom="50dp"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

    <include layout="@layout/rss_webview_bottom_sheet" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Upvotes: 0

Views: 975

Answers (1)

Eddie Brock
Eddie Brock

Reputation: 168

activity_main.xml wrap your view pager like this

<android.support.v4.widget.NestedScrollView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.v4.widget.NestedScrollView>

Fragment should be like this

  1. Just remove the app:layout_behavior="@string/appbar_scrolling_view_behavior" from recycler view

Upvotes: 0

Related Questions