【问题标题】:toolbar is not hiding automatically need to push manually to hide while scrolling in recyclerview工具栏没有自动隐藏需要手动推送才能在recyclerview中滚动时隐藏
【发布时间】:2019-03-28 19:16:41
【问题描述】:

我的应用程序包含一个带有工具栏、线性布局和 Tabllayout 的片段。我已启用工具栏的滚动标志。现在,当我尝试在 tablayout 中的 recyclerview 中滚动时,工具栏不会受到阻碍。当我手动推动标签布局时,它开始移动并且能够隐藏或显示。以下是我正在使用的布局。这里有什么问题。

主布局:

<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.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:id="@+id/app">

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

    <Button
        android:id="@+id/back"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_gravity="start"
        android:layout_marginRight="5dp"
        android:layout_marginEnd="5dp"
        android:background="@drawable/backblack" />


</android.support.v7.widget.Toolbar>

<LinearLayout
        android:id="@+id/layloc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:orientation="horizontal"
        android:weightSum="1"
        android:padding="5dp"
        android:background="@drawable/border"
    android:visibility="gone">


        <TextView
            android:id="@+id/locationname"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:drawableStart="@drawable/location"
            android:drawableLeft="@drawable/location"
            android:drawablePadding="3dp"
            android:layout_weight="1"
            android:layout_gravity="center_horizontal"
            android:textColor="@color/cblack"
            android:text="fetching current location"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium.Inverse"/>

        <TextView
            android:id="@+id/change"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Change"
            android:drawablePadding="3dp"
            android:textColor="@color/cblack"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>

    </LinearLayout>


<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/layloc"
    android:background="@color/textcolor"
    app:tabTextColor="@color/black"
    app:tabPaddingStart="0dp"
    app:tabPaddingEnd="0dp"
    app:tabIndicatorHeight="2dp" />

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



<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


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

片段布局:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/textcolor"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/mainexplore">


<android.support.v7.widget.RecyclerView
    android:id="@+id/lists"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@drawable/list_divider"
    android:dividerHeight="1px"
    android:scrollbars="none"
    android:visibility="gone"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<TextView
    android:id="@+id/emptyElement"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="@string/nofoodfound"
    android:textColor="@color/button"
    android:visibility="gone"/>

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

我尝试过使用不同的滚动标志,但问题是一样的。我在 SO 中看到了与此类似的示例和一些问题,但没有一个回答我的查询。工具栏可以隐藏但需要手动推送。布局有什么问题。此布局中缺少什么。

【问题讨论】:

  • 应用或活动的主题是什么?
  • @KaranMer - 请检查我的布局是否已经存在。
  • 我只是用代码完成的。监控滚动变化。然后显示或隐藏它。如果向下滚动,我会隐藏它,如果向上滚动,我会显示它。在滚动更改的代码中非常简单。可能会为自己节省很多时间

标签: android android-fragments android-tablayout android-coordinatorlayout


【解决方案1】:

您需要从 appBarLayout 中移除按钮视图。检查我的。

<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"
android:id="@+id/coordinator_layout"
>
<android.support.design.widget.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:elevation="5dp"
        android:theme="@style/ToolbarTheme"
        app:layout_scrollFlags="scroll|enterAlways"
        app:titleTextAppearance="@style/Toolbar.TitleText"
        />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

【讨论】:

    【解决方案2】:
    <android.support.design.widget.AppBarLayout
                android:id="@+id/appBarLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsingToolbarLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    app:expandedTitleMarginEnd="64dp"
                    app:expandedTitleMarginStart="48dp"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
                    <android.support.v7.widget.Toolbar
                        android:id="@+id/toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        android:background="@color/textcolor"
                        app:layout_collapseMode="parallax"
                        app:layout_scrollFlags="scroll|enterAlways">
    
                        <Button
                            android:id="@+id/back"
                            android:layout_width="24dp"
                            android:layout_height="24dp"
                            android:layout_gravity="start"
                            android:layout_marginEnd="5dp"
                            android:layout_marginRight="5dp"
                            android:background="@drawable/backblack" />
    
                    </android.support.v7.widget.Toolbar>
    
                </android.support.design.widget.CollapsingToolbarLayout>
    
            </android.support.design.widget.AppBarLayout>
    

    【讨论】:

    • 你想在recyclerview中向上滚动时隐藏工具栏吗?
    • 是的。对于问题中的布局。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多