Show and hide Action bar while scrolling view pager content

房东的猫 提交于 2019-12-04 08:26:31

问题


I want to show and hide the action bar /toolbar as the Google Play store app. I am using sliding tabs on top and view pager to show the content of each tab. Here is the screenshot of Google play store app for the reference:

My layout XML :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout style="@style/HeaderBar"
        android:id="@+id/headerbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include layout="@layout/toolbar_no_bg"
            android:id="@+id/toolbar_actionbar"/>

        <com.fauzie.sample.tabsspinner.widget.SlidingTabLayout
            android:background="@color/tab_background"
            android:id="@+id/sliding_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/headerbar">

    </android.support.v4.view.ViewPager>

</RelativeLayout>

Thanks in advance.


回答1:


Have a look at this:

https://github.com/flavienlaurent/NotBoringActionBar

There's a trick involved here. You'll need to:

  1. Wrap your layout in a list view
  2. Add a fake header with the same height as your action bar large header
  3. set your action bar overlay to true
  4. Translate the real header when the list moves (i.e. when the fake header is scrolled).

It's a little bit complicated at first but once you understand it, you see how smart and obvious it all is. The link will give you all you need I hope.

I myself used that link to implement it in my own app and it works great.




回答2:


Checkout this library: https://github.com/ksoichiro/Android-ObservableScrollView

this will definitely help you




回答3:


Check this example to implement this with the new official Android Design Support Library:

https://github.com/chrisbanes/cheesesquare



来源:https://stackoverflow.com/questions/26773245/show-and-hide-action-bar-while-scrolling-view-pager-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!