Tab Layout only collapsing on fling, not on slow scroll
L

1

13

for the below layout, when I scroll the content up on a page within the viewpager... if I keep my finger on the screen and scroll up slowly, the tab layout doesn't collapse into the toolbar but if I fling the content up, it does. Any ideas why?

activity_home.xml

    <layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".newnav.home.HomeActivity">

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".newnav.NavigationActivity">

        <!-- Check whether do we need this surface view still? -->
        <SurfaceView
            android:id="@+id/surfaceView"
            android:layout_width="0dp"
            android:layout_height="0dp" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <com.my.package.view.MyToolBar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:background="@color/primary_color" />

            <FrameLayout
                android:id="@+id/fragment_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/toolbar" />

            <com.my.package.view.SomeCustomView
                android:id="@+id/breaking_news_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true" />
        </RelativeLayout>

        <com.my.package.navigation.NavigationLayout
            android:id="@+id/navigation_layout"
            android:layout_width="320dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/white"
            android:fitsSystemWindows="true" />
    </android.support.v4.widget.DrawerLayout>
</layout>

fragment_home.xml (parent layout has the toolbar):

<?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">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:fitsSystemWindows="true"
            app:layout_behavior="com.nbc.news.newnav.home.NbcTabBarScrollingBehavior">

            <android.support.design.widget.TabLayout
                android:id="@+id/tabLayout"
                style="@style/HomeTabDesign"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/top_nav_tab_bg"
                app:tabBackground="@color/transparent"
                android:elevation="4dp"
                android:minHeight="@dimen/tab_layout_height"
                app:layout_scrollFlags="scroll|enterAlways"
                app:tabGravity="fill"
                app:tabIndicatorColor="@color/home_tab_strip_color"
                app:tabIndicatorHeight="3dp"
                app:tabMaxWidth="0dp"
                app:tabMode="fixed"
                app:tabSelectedTextColor="@color/home_tab_text_color_selected"
                app:tabTextColor="@color/home_tab_text_color_normal" />

        </android.support.design.widget.AppBarLayout>

        <com.my.package.view.MyViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    </android.support.design.widget.CoordinatorLayout>
</layout>

section_fragment.xml (the view that scrolls):

<?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">

    <com.my.package.view.ProgressiveFrameLayout
        android:id="@+id/progressiveLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/divider_white"
        app:contentLayoutId="@id/contentLayout">

        <FrameLayout
            android:id="@+id/contentLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v4.widget.SwipeRefreshLayout
                android:id="@+id/swipeRefreshLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/sectionRecyclerView"
                    android:layout_width="match_parent"
                    android:descendantFocusability="blocksDescendants"
                    android:layout_height="match_parent"
                    android:background="@android:color/white" />
            </android.support.v4.widget.SwipeRefreshLayout>

            <FrameLayout
                android:id="@+id/adContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|center_horizontal" />
        </FrameLayout>

    </com.nbc.news.view.ProgressiveFrameLayout>
</layout>
Langford answered 29/5, 2018 at 14:25 Comment(12)
You should probably define app:layout_collapseMode on the TabLayout.Gilgai
@RowlandMtetezi if I set the collapseMode on the tabLayout, it doesn't collapse at allLangford
have you try to remove android:fitsSystemWindows="true" ?Rodrickrodrigez
@ParaskevasNtsounos yes I haveLangford
https://mcmap.net/q/151252/-is-it-possible-to-disable-scrolling-on-a-viewpager check this answer try to disable scroll from viewpagerRodrickrodrigez
@ParaskevasNtsounos why would I disable scroll from the viewpager? I want the fragments inside the viewpager to be able to scrollLangford
@Langford can you post a video demonstrating your problem?Hexastich
@AhamadAnees unfortunately not. Again, if you keep your finger on the screen on initial load and push the content up (recycler view), the tab layout stays put. if you fling, it collapsesLangford
Hide TabLayout on Scroll of Content instead of ToolBarDvina
Can you narrow the problem by getting rid of your custom classes?Disobedience
@Disobedience there's a lot of custom happening. In a nutshell, it's activity (has toolbar) -> parent fragment (has tab layout and viewpager) -> child fragment (x2). In the child fragment, we have a swipe refresh view and a recycler viewLangford
You should put all your content with in <android.support.design.widget.CoordinatorLayout> this makes TabLayout to react to scroll events in a sibling view marked with a ScrollingViewBehavior.Zoophobia
H
1

The old answer

This solution did not work in the case. But it may be helpful to others.

You have not provided the necessary part of your code. However I strongly suspect that you are using an intermediate layout between the SwipeRefreshLayout and your Recyclerview (or ListView). In other words, SwipeRefreshLayout is not the direct parent of Recyclerview. Fix it and it will work.


If you want to know what really happens in the background

If you check the android source code, you can find a method canSwipeRefreshChildScrollUp(). It is responsible to decide whether it is possible for the child view of this layout to scroll up. It is required to override this if the child view is a custom view.

public boolean canChildScrollUp() {
        if (mChildScrollUpCallback != null) {
            return mChildScrollUpCallback.canChildScrollUp(this, mTarget);
        }
        if (android.os.Build.VERSION.SDK_INT < 14) {
            if (mTarget instanceof AbsListView) {
                final AbsListView absListView = (AbsListView) mTarget;
                return absListView.getChildCount() > 0
                        && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
                                .getTop() < absListView.getPaddingTop());
            } else {
                return ViewCompat.canScrollVertically(mTarget, -1) || mTarget.getScrollY() > 0;
            }
        } else {
            return ViewCompat.canScrollVertically(mTarget, -1);
        }
    }

If you use an intermediate view, the swipe refresh layout will assume that it is the target (see mTarget in the 6th line of the method) and the default implementation of that view return false always which will lead to this problem.

New answer

CoordinatorLayout"s child should implement NestedScrollingChild interface. So, try wrapping your FrameLayout in a NestedScrollView

Hexastich answered 12/6, 2018 at 16:27 Comment(6)
recyclerView IS a direct child of SwipeRefreshLayout but the refresh layout is not the top level view in that layout file. it's frameLayout > FrameLayout > refresh layout > recyclerviewLangford
CoordinatorLayout"s direct child should implement NestedScrollingChild interface. So, try wrapping your FrameLayout in a NestedScrollView.Hexastich
I've added the xml to the OP. making the suggested change nowLangford
still working on it. have to get recyclerview to behave inside the scroll viewLangford
I made the direct child a nestedscrollview and it collapsed immediately. The issues isn't FIXED because I need to make it work for my use case but the issue has been identified and solution is clear. Please change your answer to that and I'll give it the check. thank you for all of the help!Langford
Let us continue this discussion in chat.Hexastich

© 2022 - 2024 — McMap. All rights reserved.