Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

liftOnScroll not working with ViewPager? #163

Closed
evowizz opened this issue Jul 3, 2018 · 9 comments
Closed

liftOnScroll not working with ViewPager? #163

evowizz opened this issue Jul 3, 2018 · 9 comments

Comments

@evowizz
Copy link

evowizz commented Jul 3, 2018

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.

@cketcham
Copy link
Contributor

Moved this over to buganizer: https://issuetracker.google.com/issues/111634113

@evowizz
Copy link
Author

evowizz commented Jul 19, 2018

Thank you! 👍

@jaysowen
Copy link

jaysowen commented Aug 1, 2018

Same iusse.How to fix this?

@dsn5ft
Copy link
Contributor

dsn5ft commented Aug 1, 2018

Currently liftOnScroll expects the scrolling view to be a direct child of the CoordinatorLayout. The workaround is to not set app:liftOnScroll on your AppBarLayout and instead to call setLiftable(true) and setLifted() based on when your scrolling view is scrolled. Here is an example using a ListView:

appBarLayout.setLiftable(true);
listView.setOnScrollListener(
    new OnScrollListener() {
      @Override
      public void onScrollStateChanged(AbsListView view, int scrollState) {}

      @Override
      public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        boolean isAtTop = firstVisibleItem == 0 && (view.getChildCount() == 0 || view.getChildAt(0).getTop() == 0);
        appBarLayout.setLifted(!isAtTop);
      }
    });

@Ioane5
Copy link

Ioane5 commented Aug 27, 2018

It is strange that AppBarLayout collapse/expand works with ViewPager, but this requires hack.

@seth-gravy
Copy link

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).

@abunayem
Copy link

How to enable liftOnScroll in a fragment?

@khaykov
Copy link

khaykov commented 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?

@OxygenCobalt
Copy link
Contributor

OxygenCobalt commented Aug 22, 2021

Sorry for reviving this thread, but does anyone know how to get this working with a TabLayout within an AppBarLayout that collapses on scroll? I'm doing what seth-gravy said, and it works in all cases EXCEPT when switching tabs using a TabLayout
when the AppBarLayout is collapsed. Swiping works, switching tabs when the AppBarLayout is expanded works, theres no problems BUT this. Should I file a bug report?

Edit: Solved this issue, turns out I needed to increase the ViewPager's offscreenPageLimit to the size of all items so that the state would not be confused by recycling fragments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants