【问题标题】:Gridview not showing/hiding toolbarGridview不显示/隐藏工具栏
【发布时间】:2015-08-18 00:07:57
【问题描述】:

我正在使用设计支持库并使用GridView,但是当我向上或向下滚动到网格时,Toolbar 没有隐藏或显示。我在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/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.Dark.ActionBar">


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

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

    <GridView
        android:id="@+id/gridView1"
        android:numColumns="2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

这是我活动中的 *.xml 文件。我只是 setSupportActionBar(toolbar) 并初始化了 GridView 适配器。

【问题讨论】:

    标签: android gridview android-design-library


    【解决方案1】:

    目前ListViewGridView 具有CoordinatorLayout 的预期行为,只有API>21

    要获得此行为,您必须设置:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        gridView.setNestedScrollingEnabled(true);
    }
    

    您可以找到更多信息here

    【讨论】:

      【解决方案2】:

      假设您使用CoordinatorLayout 作为父布局,则需要使用RecyclerViewNestedScrollView 才能使其正常工作

      【讨论】:

      • 尝试将 GridView 包含在 NestedScrollView 中。从目前用户所报告的情况来看,只有 RecyclerView 和 NestedScrollView 与 CoordinatorLayout 正确合作
      • 我能想到的唯一其他解决方案是(如果可能的话)在实现 GridLayoutManager 的 RecyclerView 中转换您的 GridView
      猜你喜欢
      • 2023-03-26
      • 2012-01-07
      • 1970-01-01
      • 2017-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-17
      相关资源
      最近更新 更多