question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

liftOnScroll not working with ViewPager?

See original GitHub issue

Overview

When I set app:liftOnScroll="true" to my AppBarLayout, and then launch the app, I don’t see any shadow, and scrolling down or up doesn’t work.

My Layout:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/coordinatorLayoutRoot"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

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

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

        <com.google.android.material.tabs.TabLayout
          android:id="@+id/tabs"
          style="@style/TabLayout.Style"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>

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

    <androidx.viewpager.widget.ViewPager
      android:id="@+id/viewPager"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:overScrollMode="always"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Version number

com.google.android.material:material:1.0.0-beta01

Operating system and device

  • OnePlus 6 with Android 8.1
  • Emulator (Pixel 2 XL) Android P (latest DP)

Related issues

Someone already posted a similar issue (#140 ) but his issue was fixed by removing the bottom margin of his NestedScrollView. However I’m using a ViewPager, and I don’t have any bottom margin in my layout.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
seth-gravycommented, Jul 29, 2019

You can now set liftOnScrollTargetViewId on your AppBarLayout to point to your ViewPager directly. Or in my case, a RecyclerView inside of the ViewPager (but make sure each RecyclerView has a unique id and you update liftOnScrollTargetViewId in the ViewPager’s onTabSelected).

4reactions
khaykovcommented, Jul 28, 2020

If by any chance you have multiple fragments with the same layout in ViewPager, having unique IDs for scroll containers and communicating them to AppBar is a whole new can of headache.

While changing ID of view after getting an initial reference to them (using findViewById or with KTX view binding) works, I can’t help but feel like it’s going to come and bite me later, or not me, but another unsuspecting developer who will try to get the view using its original ID for whatever reason.

The safest way to do it, that I’m aware of, is by having an ID for each fragment in ViewPager stored in ids.xml. This adds a lot of manual labor, especially if you have a good number of ViewPager (also, it will not work with dynamically added fragments, if that’s your things).

Anything else I’m missing?

Read more comments on GitHub >

github_iconTop Results From Across the Web

[AppBarLayout] liftOnScroll not working with ViewPager ...
When I set app:liftOnScroll="true" to my AppBarLayout, and then launch the app, I don't see any shadow, and scrolling down or up doesn't...
Read more >
Androidx ViewPager not working with scrolling view behavior
But I've been playing with the liftOnScroll, both programatically and just on xml. Gonna update the issue. – 4gus71n. Nov 1, 2018 at...
Read more >
Handling Scrolls with CoordinatorLayout - CodePath Cliffnotes
The first step is to make sure you are not using the deprecated ActionBar. ... Keep in mind that ScrollView does not work...
Read more >
RecyclerView to ViewPager2 with MaterialViewPager | Android
Create ViewPager adapter; 6. ... let's copy the strings for showing titles and other data, colors, drawables and styles for the app.
Read more >
How to add CastButton to MaterialToolbar (not appcompat ...
[Solved]-How to add CastButton to MaterialToolbar (not appcompat Toolbar)-kotlin ... app:liftOnScroll="true"> <com.google.android.material.appbar.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found