2016-01-04 105 views
0

我嘗試使工具欄的滾動行爲體現在Google照片中。但我的工具欄沒有完全隱藏在狀態欄下。如何在滾動回滾狀態欄時完全隱藏狀態欄上的工具欄

enter image description here

enter image description here

enter image description here

<android.support.design.widget.CoordinatorLayout 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="match_parent"> 
<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 
    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/colorPrimary" 
     app:layout_scrollFlags="scroll|enterAlways|snap" /> 
</android.support.design.widget.AppBarLayout> 
<android.support.v7.widget.RecyclerView 
    android:id="@+id/rv" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clipToPadding="false" 
    android:clipChildren="false" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

回答

0

我使用這段代碼來隱藏工具欄,你可以從下面的代碼中獲得幫助。

public class MainActivity extends AppCompatActivity implements ObservableScrollViewCallbacks{ 


    @Override 
    public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) { 

    } 

    @Override 
    public void onDownMotionEvent() { 

    } 

    @Override 
    public void onUpOrCancelMotionEvent(ScrollState scrollState) { 
     ActionBar ab = getSupportActionBar(); 
     if (scrollState == ScrollState.UP) { 
      if (ab.isShowing()) { 
       ab.hide(); 
      // image.setVisibility(View.GONE); 
      } 
     } else if (scrollState == ScrollState.DOWN) { 
      if (!ab.isShowing()) { 
       ab.show(); 
      // image.setVisibility(View.VISIBLE); 
      } 
     } 

    } 

    ImageView image; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // image = (ImageView) findViewById(R.id.image); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     ObservableListView listView = (ObservableListView) findViewById(R.id.list); 
     listView.setScrollViewCallbacks(MainActivity.this); 

     ArrayList<String> items = new ArrayList<String>(); 
     for (int i = 1; i <= 100; i++) { 
      items.add("Item " + i); 
     } 
     listView.setAdapter(new ArrayAdapter<String>(
       this, android.R.layout.simple_list_item_1, items)); 


    } 
    }