【问题标题】:Show toolbar after hidden by scrolling on button click通过单击按钮滚动隐藏后显示工具栏
【发布时间】:2016-02-03 12:20:15
【问题描述】:

我在堆栈溢出中发现了这个问题,但没有合适的解决方案。
Programmatically show Toolbar after hidden by scrolling (Android Design Library)

我在工具栏中有一个自定义视图,当我上下滚动时,工具栏会显示和隐藏。
当我向上滚动时,工具栏将隐藏,我在 ActionBar 菜单中显示一个图标。当我单击该菜单时,我想以相同的动画显示隐藏的工具栏及其内容。
我试图翻译工具栏但没有用。
这是我的带有自定义视图的工具栏代码。

<?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/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="3dp"
    android:layout_marginBottom="3dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_marginRight="15dp"
            android:background="@drawable/searchbox_bg"
            android:gravity="center"
            android:orientation="horizontal"
            android:weightSum="2">

            <EditText
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:hint="@string/home_search_hint"
                android:maxLines="1"
                android:textSize="15dp"
                android:singleLine="true"
                android:id="@+id/home_search"
                android:drawableStart="@android:drawable/ic_menu_search"
                android:drawableLeft="@android:drawable/ic_menu_search"
                android:drawablePadding="3dp"
                android:drawableTint="@color/gray_2"
                android:drawingCacheQuality="high"
                android:paddingLeft="10dp"
                android:background="@drawable/searchbox_bg"
                android:layout_weight="2"
                />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:id="@+id/home_speak"
                android:background="@drawable/searchbox_bg"
                android:src="@android:drawable/ic_btn_speak_now"/>
        </LinearLayout>
        </android.support.v7.widget.Toolbar>


    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/transparent"
        android:visibility="invisible"/>


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

【问题讨论】:

  • 您尝试过的任何Java 代码?请把它们放在问题中。
  • @LinX64:在java代码中,除了加载数据到RecyclerView之外别无他法。如果需要,我会提供代码。
  • 所以,你什么都没尝试。
  • @LinX64:将我视为初学者。工具栏自动上升可能是因为协调器布局。上下移动工具栏没有具体的代码。
  • 因为app:layout_scrollFlags="scroll|enterAlways"

标签: android android-recyclerview android-toolbar android-coordinatorlayout android-appbarlayout


【解决方案1】:

要以编程方式展开或折叠工具栏,您应该使用

setExpanded(boolean expanded, boolean animate) 

方法见Doc

让我们来回答你的问题,以便以编程方式扩展工具栏

展开工具栏

AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.appBarLayout);
        appBarLayout.setExpanded(true, true);

如果你想折叠它的使用:

AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.appBarLayout);
        appBarLayout.setExpanded(false, true);

希望对你有帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多