henrytao-me / smooth-app-bar-layout

Smooth version of Google Support Design AppBarLayout
Apache License 2.0
1.77k stars 240 forks source link

AppBarLayout.setExpanded(bool) causes strange behaviour #183

Closed vszakd closed 7 years ago

vszakd commented 7 years ago

Hello, I'm experiencing a strange issue that does not happen with the regular support AppBarLayout. This is the main Activity layout:

<?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/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment_placeholder" />
    <me.henrytao.smoothappbarlayout.SmoothAppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        >
        <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                app:layout_scrollFlags="enterAlways|scroll"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin" />
    </me.henrytao.smoothappbarlayout.SmoothAppBarLayout>
</android.support.design.widget.CoordinatorLayout>

Fragment B layout

<android.support.v4.widget.NestedScrollView
    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/nested_scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <View
        android:layout_width="match_parent"
        android:layout_height="50dp" />
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    app:layoutManager="LinearLayoutManager"
    tools:listitem="@layout/fragment_services" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>

Now let's say that fragment_placeholder has fragment A shown. We then replace it via a transaction with fragment B, which calls AppBarLayout.setExpanded(true) in its onCreateView() callback. Here it comes the problem: if the Toolbar was shown in fragment A, then in B the behaviour is as expected. On the contrary, if in A the Toolbar was hidden (e.g.: by scrolling up with the finger), the Toolbar in B does get shown correctly but if a down slide occurs the Toolbar becomes cut in half. If a subsequent up slide is done the behaviours returns normal. Same to be said in case of a first non-down slide: no problems at all.

Any ideas? If more information is needed I'm here to help. Thank you.

Attachments

Fragment B is shown correctly, with AppBarLayout expanded img_20170201_010432

A down slide with the finger is done. img_20170201_010502

henrytao-me commented 7 years ago

Hi @mollicaw

Please try to call smoothAppBarLayout. syncOffset(0); to reset scrolling position after switching to fragment B. Also notice that, you have RecyclerView inside NestedScrollView in fragment B, please call recyclerView.setNestedScrollEnabled(false);.

Let me know if it works for you. Thanks.

vszakd commented 7 years ago

Hello @henrytao-me, thanks for your suggestion! recyclerView.setNestedScrollEnabled(false); was already present and syncOffset(0); resolved the problem, but only partially. The previous use-case is now solved but there is a new problem, here is how to reproduce it:

  1. Stay on fragment A and let the toolbar disappear scrolling down
  2. Do something to switch to fragment B
  3. OK! Everything seems to work well.
  4. Scroll down (finger up) the RecyclerView and WHILE the page is scrolling press Android's back button (physical or on-screen)
  5. You are now on fragment A, do the same action you did before to go to fragment B
  6. The Toolbar is not present! And if you scroll up (finger down) the original problem (kind-of half shown Toolbar) comes into play. If you scroll the page down then up everything returns normal.

This behaviour does not happen with the support AppBarLayout. Any thoughts? Thanks in advance.

henrytao-me commented 7 years ago

Hi @mollicaw

It looks like you use backstack with fragment. In that case, you may need to reset scrolling position (by calling setSyncOffset(0)) when backstack is changed (doing backpress).

Let me know if you need further assistant.

Cheers, Henry