0

How to hide toolbar, when scrolling webview? I know, that webview must be in NestedScrollView and CoordinatorLayout, toolbar - in AppBarLayout.But I really cant to do it.

If anybody can help me, I would be very grateful.

activity_main.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"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/main_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/custom_toolbar_size" />

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


    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/custom_tablayout_size"
        android:layout_gravity="bottom"
        android:background="@color/toolbarColor"
        app:tabIndicatorHeight="0dp"
        >

        <android.support.design.widget.TabItem
            android:id="@+id/ti_arrow_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/ic_arrow_back" />

        <android.support.design.widget.TabItem
            android:id="@+id/ti_arrow_forward"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/ic_arrow_forward" />

        <android.support.design.widget.TabItem
            android:id="@+id/ti_update"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/ic_update" />

        <android.support.design.widget.TabItem
            android:id="@+id/ti_inset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/ic_inset" />

        <android.support.design.widget.TabItem
            android:id="@+id/ti_star"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/ic_star_border" />

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:layout_marginBottom="@dimen/custom_tablayout_size"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/custom_toolbar_size"
    android:background="@color/toolbarColor"
    android:paddingTop="2dp"
    app:layout_scrollFlags="scroll|enterAlways" />
Ilya
  • 106
  • 10

2 Answers2

0

The solutions you have been trying didn't work because because your WebView is inside a NestedScrollView. An OnScrollListener for your WebView won't work too because of that reason.

So use a NestedScrollView listener, then use getSupportActionBar().hide(); and getSupportActionBar().show(); to hide or show your toolbar.

Copied from here: https://stackoverflow.com/a/37630070/1860982

        final String TAG = "ScrollPosition";
        NestedScrollView nestedScrollView = (NestedScrollView) findViewById(R.id.scrollNextView);
        if (nestedScrollView != null) {

            nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
                @Override
                public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

                    if (scrollY > oldScrollY) {
                        getSupportActionBar().hide();
                        Log.i(TAG, "Scroll DOWN");
                    }
                    if (scrollY < oldScrollY) {
                        Log.i(TAG, "Scroll UP");
                    }

                    if (scrollY == 0) {
                        getSupportActionBar().show();
                        Log.i(TAG, "TOP SCROLL");
                    }

                    if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
                        Log.i(TAG, "BOTTOM SCROLL");
                    }
                }
            });
        } 
Community
  • 1
  • 1
Tosin Onikute
  • 3,403
  • 3
  • 33
  • 57
  • Thank you for the answer, it works, but lags, when i try to scroll. how to make scroll smoother? – Ilya Dec 01 '16 at 16:25
-1

You can do this without any Java code using the design library's CoordinatorLayout and NestedScrollView. Here's how you do it.