簡體   English   中英

隱藏/顯示工具欄

[英]Hide/show Toolbar

在我的應用程序中,當列表滾動時,我想隱藏/顯示工具欄。 我認為,我實現了以下鏈接中顯示/隱藏的所有內容:

https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling%28part3%29/

問題是,滾動列表時,工具欄沒有移動/隱藏。

這是.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/main_content"
    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.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:titleTextColor="@android:color/white" />
    </android.support.design.widget.AppBarLayout>

    <se.emilsjolander.stickylistheaders.StickyListHeadersListView
        android:id="@+id/lvRef"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

這里是依賴項:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'se.emilsjolander:stickylistheaders:2.7.0'
    compile 'com.android.support:recyclerview-v7:23.4.0'
    compile 'com.android.support:cardview-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
}

不幸的是,無法在ListView上進行嵌套滾動。 請改用粘性標頭RecyclerView。

如果要隱藏向下滾動的工具欄,則首先檢測ListView或RecyclerView的滾動dy ,然后再從工具欄獲取actionBar對象。

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

    //in on scroll listener of your view check for dy

    if (dy > 0) {
        //detecting scroll up action  
        toolbar.setVisibility(View.GONE);
    } else {
        //detect scrolldown action dy<0
        toolbar.setVisibility(View.VISIBLE);
    }

在工具欄的布局中:

使用android:layout_height="?attr/actionBarSize"代替android:layout_height="wrap_content"並刪除android:minHeight="?attr/actionBarSize"

要隱藏工具欄,您可以這樣做

toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();

要再次顯示工具欄,請按照以下方式進行操作

toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM