0

如何隱藏工具欄,滾動網頁流量是什麼時候? 我知道,該webview必須在NestedScrollView和CoordinatorLayout工具欄 - 在AppBarLayout.But我真的不能這樣做。不能老是隱藏工具欄時的WebView滾動

如果有人能幫助我,我將非常感激。

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" /> 
+0

看到這個:http://stackoverflow.com/questions/28770530/how-to-hide-actionbar-toolbar-while-scrolling-down-in-webview –

+0

@HtmlTosin我看過它,但它doesn'幫助我。我已經嘗試了幾次 – Ilya

+0

然後你應該使用onScrollListener並手動隱藏工具欄 –

回答

0

你一直在努力,因爲你的WebView是NestedScrollView裏邊沒有因爲工作的解決方案。由於這個原因,WebView的OnScrollListener不會工作。

因此,使用NestedScrollView偵聽器,然後使用getSupportActionBar().hide();getSupportActionBar().show();隱藏或顯示您的工具欄。

從這裏複製: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"); 
        } 
       } 
      }); 
     } 
+0

謝謝你的答案,它的工作原理,但滯後,當我嘗試滾動。如何使滾動更順暢? – Ilya

-1

你可以做到這一點,而無需使用設計庫的CoordinatorLayout和NestedScrollView任何Java代碼。這是你如何做到的。

+0

「這是你怎麼做」缺少一個實際的解釋如何? –

+0

我知道,我可以在不使用任何Java代碼的情況下通過使用設計庫的組件來實現它。正如你見過我的問題,我試圖這樣做 - 不成功。 – Ilya

+0

一旦掌握了它,你可以使用不同的layout_scrollFlags和適合系統Windows行爲。 –

相關問題