Abhishek Singh
Abhishek Singh

Reputation: 9188

Scrolling Recycler View programmatically does not update CoordinatorLayout.Behaviour while scrolling it by touch update CoordinatorLayout.Behaviour

I referred This Question but no answers works for me. So basically I have a BottomNavigationView which hide on scroll down and show on scroll up. it works fine when I scroll Recycler View by touch but does not work when I scroll the RecyclerView programmatically. Seems like it does not trigger CoordinatorLayout.Behaviour when programmatically scrolled. In a certain case I have to open that fragment which contains RecyclerView with scroll to some position, and BottomNavigationView hiding it have to came back when user scroll up That means I don't want to hide visibility of BottomNavigationView just hide on scroll either scroll by programmatically or by touch.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.kliff.digitaldwarka.activity.MainActivity">
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:id="@+id/myAppBar"
            app:elevation="0dp"
            android:theme="@style/AppTheme.AppBarOverlay">
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:contentInsetStart="0dp"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>
        </android.support.design.widget.AppBarLayout>
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

        <android.support.design.widget.BottomNavigationView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:itemBackground="@android:color/white"
            android:id="@+id/bottom_nav"
            android:layout_gravity="bottom"
            app:menu="@menu/bottom_nav_menu"/>
    </android.support.design.widget.CoordinatorLayout>
    <com.appeaser.sublimenavigationviewlibrary.SublimeNavigationView
    android:id="@+id/nav_view"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_gravity="start"
    app:snvHeaderLayout="@layout/nav_header"
    app:snvMenu="@menu/nav_menu"
    style="@style/NavigationViewStyle"
    app:snvItemTextColor="#555555"
    app:snvSubheaderItemTextColor="#555555"
    app:snvItemIconTint="#555555"
    app:snvBadgeTextColor="#555555"
    app:snvHintTextColor="#757575"
    app:snvHintTypefaceStyle="normal"
    app:snvBadgeTypefaceStyle="normal"
    app:snvSubheaderItemTypefaceStyle="normal"
    app:snvItemTypefaceStyle="normal" />
</android.support.v4.widget.DrawerLayout>

fragment_category.xml

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="1dp"
    app:cardUseCompatPadding="true">
    <android.support.v7.widget.RecyclerView
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />
</android.support.v7.widget.CardView>

Code For scrolling

 mRecyclerView.getLayoutManager().scrollToPosition(34); //specific postion

By scrolling this way how can I achive the behaviour. Any Help Would be appricited thanks.

Upvotes: 2

Views: 1124

Answers (4)

Herbert Hartkor
Herbert Hartkor

Reputation: 41

For me nothing did work, until I wrapped Bhavesh Jabuvanis answer in a post

mRecyclerView?.post  {
  mRecyclerView?.startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL, ViewCompat.TYPE_NON_TOUCH)
  mRecyclerView?.smoothScrollToPosition(scrollToPosition)
}

Upvotes: 1

Bhavesh Jabuvani
Bhavesh Jabuvani

Reputation: 359

CoordinatorLayout.Behaviour is works with only NestedScroll event. When you try to scroll RecyclerView programmatically it is treat as normal scroll.

Write below line to inform RecyclerView start NesteadScroll, With ViewCompat.SCROLL_AXIS_VERTICAL and ViewCompat.TYPE_NON_TOUCH

ViewCompat.SCROLL_AXIS_VERTICAL: Indicates scrolling along the vertical axis. ViewCompat.TYPE_NON_TOUCH: Indicates that the input type for the gesture is caused by something which is not a user touching a screen. This is usually from a fling which is settling.

recycler_view.startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL, ViewCompat.TYPE_NON_TOUCH)
recycler_view.smoothScrollBy(0,200)

Upvotes: 3

Vaibhav Solanki
Vaibhav Solanki

Reputation: 34

I have use this code please check if its suitable for you :-

activity_mail.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.constraintlayout.ScrollingActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@android:drawable/ic_dialog_email" />

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

content_scrolling.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.constraintlayout.ScrollingActivity"
    tools:showIn="@layout/activity_scrolling">

   <android.support.v7.widget.RecyclerView
       android:id="@+id/recycler_view"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:scrollbars="vertical" />

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

Upvotes: -1

Soo Chun Jung
Soo Chun Jung

Reputation: 595

Try the following code. Its working for me. I am using the BottomBar Library.

public void showBottomBar(boolean show) {
    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mBottomBar.getLayoutParams();
    CoordinatorLayout.Behavior behavior2 = params.getBehavior();
    if (behavior2 != null) {
        behavior2.onNestedFling(coordinator, mBottomBar, null, 0, show ? -10000 : 10000, true);
    }
}

Upvotes: 0

Related Questions