隐藏工具栏行为,例如中型android应用程序

时间:2019-05-27 17:30:37

标签: android android-coordinatorlayout

我正在尝试达到这种效果(就像中等的android app工具栏一样),我想隐藏一个自定义视图,不幸的是我无法做到。这是我的布局。

https://i.ibb.co/KNp1zfV/2019-05-27-14-23-47.gif

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    app:elevation="0dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/filtersContainerLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/left_rectangle_edge_view_size"
        android:background="@drawable/custom_rectangle_ripple_background"
        android:paddingBottom="@dimen/default_padding_extra_small"
        android:orientation="vertical"
        app:layout_scrollFlags="scroll|enterAlways">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/resultMessageTextView"
            style="@style/DefaultTextStyle.Black"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/separator_gray"
            android:drawableEnd="@drawable/ic_pencil_edit"
            android:drawablePadding="@dimen/default_margin_small"
            android:paddingStart="@dimen/default_margin_medium"
            android:paddingTop="@dimen/default_padding"
            android:paddingEnd="@dimen/default_padding_large"
            android:paddingBottom="@dimen/default_padding"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Searching for family medicine near Montevideo" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/filtersHeaderTextView"
            style="@style/DefaultTextStyle.Black"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/default_margin_medium"
            android:layout_marginTop="@dimen/default_margin_extra_small"
            android:text="@string/filters"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/resultMessageTextView" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/expandCollapseFilterOptionsImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?actionBarItemBackground"
            android:padding="@dimen/default_margin_small"
            app:layout_constraintBottom_toBottomOf="@+id/filtersHeaderTextView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/filtersHeaderTextView"
            android:layout_marginEnd="@dimen/default_margin"
            app:srcCompat="@drawable/ic_add"
            app:tint="@color/colorPrimaryDark" />

        <LinearLayout
            android:id="@+id/filtersButtonsLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/default_margin_small"
            android:orientation="horizontal"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="@+id/filtersHeaderTextView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/expandCollapseFilterOptionsImageView"
            app:layout_constraintTop_toTopOf="@+id/filtersHeaderTextView"
            tools:visibility="visible">

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/applyFilterButton"
                style="@style/SmallTextStyle.Bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/default_padding_small"
                android:text="@string/apply"
                android:textColor="@drawable/text_selector_primary_dark" />

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/clearFiltersButton"
                style="@style/SmallTextStyle.Bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/default_margin_small"
                android:padding="@dimen/default_padding_small"
                android:text="@string/clear"
                android:textColor="@drawable/text_selector_primary_dark" />
        </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>

    <View
        android:id="@+id/topSeparatorView"
        android:layout_width="match_parent"
        android:layout_height="@dimen/separator_height"
        android:layout_marginStart="@dimen/left_rectangle_edge_view_size"
        android:background="@color/separator_gray"
        app:layout_scrollFlags="snap" />
</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
    android:id="@+id/resultContainerScrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:overScrollMode="never"
    app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/providersResultRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/providers_result_margin_start"
            android:overScrollMode="never" />
    </LinearLayout>
</androidx.core.widget.NestedScrollView>

1 个答案:

答案 0 :(得分:1)

  

This thread on medium可能完全满足您的需求,如果您想深入了解自定义行为,请使用another one

现在回答:

1。 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/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    <android.support.design.widget.AppBarLayout
        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"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.AppBarLayout>

    <!-- call the content xml file-->
    <include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

1。 content_main.xml

<android.support.v4.widget.NestedScrollView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:id="@+id/textview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="hello world"/>

            <!-- your content goes here-->

    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

3。重要说明

  
      
  • content_main.xml使用: android.support.v4.widget.NestedScrollView 代替ScrollView

  •   
  • 使用 app:layout_behavior="@string/appbar_scrolling_view_behavior" 内部 android.support.v4.widget.NestedScrollView,如下所示。

  •   
  • 如果并且仅 如果,如果您迁移到AndroidX,请确保更改设计支持库仍在使用android.support保持一切不变。

  •   

更新

尝试以下更改:

1。。将此添加到您的ToolBar

app:layout_scrollFlags="scroll|enterAlways"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 

2。。将此添加到您的NestedScrollLayout

android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"