How to hide ActionBar while scrolling ListView in android?

后端 未结 4 593
南旧
南旧 2020-12-10 01:23

I need to create a GUI with a ListView and an ActionBar which will hide when scrolling down and when scrolling up it must reappear.

The foll

相关标签:
4条回答
  • 2020-12-10 01:45

    you should use CoordinatorLayout for this task. It is part of the support design library. Here, in the CoordinatorLayout and the app bar section, you can find an example

    0 讨论(0)
  • 2020-12-10 01:56

    If you would like to obtain a list with this behaviour, you should:

    • add the design support library with compile 'com.android.support:design:22.2.0'
    • Use a CoordinatorLayout with a Toolbar where you have to define app:layout_scrollFlags="scroll|enterAlways"
    • Use a RecyclerView instead of a ListView. As described here ListView and the GridView have the expected behavior with the CoordinatorLayout only with API>21. In this case you have to use setNestedScrollingEnabled(true);

    The official blog post shows this case:

    <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">
    
         <! -- Your Scrollable View -->
        <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
        <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
              <android.support.v7.widget.Toolbar
                      ...
                      app:layout_scrollFlags="scroll|enterAlways">
    
    
         </android.support.design.widget.AppBarLayout>
    </android.support.design.widget.CoordinatorLayout>
    
    0 讨论(0)
  • 2020-12-10 01:57

    I would recommend using the new support design library by Google.

    Include it in your dependecies:

    compile 'com.android.support:design:22.2.0'
    

    and then use the AppBarLayout together with NestedScrollView.

    For your Toolbar define app:layout_scrollFlags="scroll|enterAlways", which says it will disappear as you scroll, and come back immediately if you scroll up (meaning you don't have to scroll all the way up).

    <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="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways" />
    
    </android.support.design.widget.AppBarLayout>
    
    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </android.support.v4.widget.NestedScrollView>
    

    0 讨论(0)
  • 2020-12-10 02:04

    Use [CoordinatorLayout]:https://developer.android.com/reference/android/support/design/widget/CoordinatorLayout.html, which allow co-oridanation among child views. it's like, act(AppBarLayout->scrolling) on some view when there is a behaviour observed(ListView->scroll) in another view.

    1. Make Listview nestedScrollingEnabled, works for >API 21

      android:nestedScrollingEnabled="true"
      
    2. Trigger layout behaviour to appbar scrolling.

      android:nestedScrollingEnabled="true"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      
    3. Any layout(ToolBar/TabLayout/any), which is required to show-hide/scroll, place it inside AppBarLayout, and enabled scroll flag.

      app:layout_scrollFlags="scroll|enterAlways"
      
    0 讨论(0)
提交回复
热议问题