【问题标题】:Android AppBarLayout overlaps listviewAndroid AppBarLayout 与 listview 重叠
【发布时间】:2015-09-18 13:51:46
【问题描述】:

我正在编写一个简单的应用程序来使用 ContentProvider,我有一个数据库、一个 ContentProvider、一个主要活动、一个使用 ContentResolver 将命令转发到 ContentProvider 的类。在 gui 上,我只想显示存储在数据库中的所有项目。我从头开始创建了这个项目,在创建 Activity 时,主布局有一个 CoordinatorLayout,带有一个 AppBarLayout,这很好,我创建了一个 ListView,除了 AppBarLayout 与 ListView 重叠,在 listview 的第一项下方被 AppBarLayout 隐藏。

我尝试在我的 ListView 中使用 android:layout_below,但它不起作用,如果我使用 android:layout_marginTop,那么我的 ListView 在 AppBarLayout 下,但我不觉得这个解决方案很好。

在 AppBarLayout 下有没有干净简单的方法让 ListView ?

在我的 activity_main 布局下方:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/holo_light_background"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <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/AppTheme.AppBarOverlay"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.design.widget.AppBarLayout>


    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/bar"/>


    <android.support.design.widget.FloatingActionButton ...>

    <android.support.design.widget.FloatingActionButton ...>

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

【问题讨论】:

  • 您没有显示足够的 XML,共享整个布局文件。
  • 另外,你不应该有超过 1 个 FAB !!!
  • @Booger 实际上是整个 xml。最后一行丢失了,我添加了它,我没有给出整个浮动按钮的定义。关于 2 个浮动按钮的使用,我想你是对的,谷歌建议只使用一个浮动按钮,但他们有时会使用 2 个浮动按钮 [google.com/design/spec/components/…,这对我来说不是很清楚
  • Re FAB - 当然不适合像删除所有内容这样的破坏性操作。不过请阅读:medium.com/tech-in-asia/…
  • 我是这方面的专家(查看我的简历),您应该只使用一个 FAB,当您单击 FAB 时,可以看到其他操作会从中暴露出来,这会有所不同。

标签: android listview android-appbarlayout


【解决方案1】:

简单的添加

app:layout_behavior="@string/appbar_scrolling_view_behavior"

到您的列表视图

【讨论】:

  • 如果您的意思是我必须将 layout_behavior 添加到 ListView 中,那么您是对的;)
  • @OLeeCsobert 如果有效,则仅适用于 Lollipop 及更高版本。到目前为止,还没有用于实现所需行为的嵌套滚动支持。我建议您迁移到 RecyclerView,因为它通过支持库具有此功能。
  • @Eugen Pechanec,感谢您的评论,这实际上是我问自己的一个问题:我们是否应该完全放弃 ListView 只使用 RecyclerView?
  • @OLeeCsobert 好问题。目前,我将 ListView 保留在一些遗留代码中,并且当我需要过滤器和 SectionIndexers 时,因为它们与 ListView 一起使用是开箱即用的(尽管它与可折叠的应用栏不兼容)。在新项目或升级代码中,我只使用 RecyclerViews。
  • 我在 Nexus 4 中使用 Android 5.1.1 进行了检查,但问题没有得到解决。
【解决方案2】:

另一种方法是使用 RecyclerView 而不是 ListView,它肯定会工作........

使用:

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

显然它只适用于 Lollipop。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 2016-10-14
    • 2015-12-22
    • 1970-01-01
    相关资源
    最近更新 更多