MotassemJa February 2016

Hiding/Showing the action bar when scrolling

I'm using ObservableRecyclerView in a Fragment (I'm using fragments because I have tabs in my app). I need the action bar to be hidden/shown based on the scroll of the ObservableRecyclerView. I followed the answer in this question, but it is a bit misleading since I couldn't understand what oldScrollY is.

I tried also this code but it is hiding/showing the bar without animation

@Override
    public void onUpOrCancelMotionEvent(ScrollState scrollState) {
        ActionBar ab = ((AppCompatActivity) getActivity()).getSupportActionBar();
        if (scrollState == ScrollState.UP) {
            if (ab.isShowing()) {
                ab.hide();
            }
        } else if (scrollState == ScrollState.DOWN) {
            if (!ab.isShowing()) {
                ab.show();
            }
        }
    }

EDIT:

activity_main.xml

<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:id="@+id/coordinator_layout_all"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/toolbarContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/primary"
            android:elevation="2dp"
            android:minHeight="?attr/actionBarSize"
            app:theme="@style/T        

Answers


mahmoud moustafa February 2016

You can use coordinatorlayout in your .xml file that contain toolBar , see below code:

   <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/all"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:orientation="vertical"
    android:fitsSystemWindows="true">
    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|enterAlways"
        >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"
        app:layout_scrollFlags="scroll|enterAlways"
        app:layout_collapseMode="pin"
        />
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/content_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >
    </FrameLayout>

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

Post Status

Asked in February 2016
Viewed 3,400 times
Voted 7
Answered 1 times

Search




Leave an answer