Android AppBarLayout 重叠 ListView

17
我正在编写一个简单的应用程序来使用ContentProvider,我有一个数据库、一个ContentProvider、一个主Activity和一个将命令转发给ContentProvider使用ContentResolver的类。在GUI上,我只想显示存储在数据库中的所有项目。我从头开始创建了这个项目,当创建Activity时,主布局具有CoordinatorLayout和AppBarLayout,这很好,我创建了ListView,一切正常,但是AppBarLayout重叠了ListView,列表视图的第一个项在AppBarLayout下方被隐藏。 enter image description here 我尝试为我的ListView使用android:layout_below,但它不起作用,如果我使用android:layout_marginTop,那么我的ListView就位于AppBarLayout下面,但我认为这不是一个好的解决方案。
没有干净简单的方法可以使ListView位于AppBarLayout下面吗?
下面是我的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>

1
你没有展示足够的XML代码,请分享整个布局文件。 - Booger
3
还有,您不应该拥有超过1个FAB!!! - Booger
1
@Booger 实际上是整个xml文件。最后一行缺失,我已经添加了它,但我没有提供整个floatingbuttom的定义。关于使用两个浮动按钮,我想你是对的,Google建议只使用一个浮动按钮,但他们有时会使用两个浮动按钮[https://www.google.com/design/spec/components/buttons-floating-action-button.html#buttons-floating-action-button-transitions],这对我来说不是很清楚。 - eqtèöck
关于 FAB - 绝对不要用于像删除所有内容这样的破坏性操作。不过请阅读这篇文章:https://medium.com/tech-in-asia/material-design-why-the-floating-action-button-is-bad-ux-design-acd5b32c5ef - Eugen Pechanec
我是这方面的专家(请查看我的个人简介),你应该只使用一个FAB,当你点击FAB时,会有一种暗示,其他操作也会从中暴露出来,这将是不同的。 - Booger
如果您使用两个FAB,则应该像Google Maps一样使用它们。一个在另一个之上。 - Joaquin Iurchuk
2个回答

36

简单加法

app:layout_behavior="@string/appbar_scrolling_view_behavior"

到您的ListView


1
如果您的意思是让我在ListView中添加'layout_behavior',那么您是正确的。 - eqtèöck
1
如果它能够工作,它只能在Android 5.0及以上版本上工作。在此之前,没有嵌套滚动支持,而这正是实现所需行为的方法。我建议你转移到RecyclerView,因为它通过支持库具备了这种功能。 - Eugen Pechanec
@Eugen Pechanec,感谢您的评论,这实际上是我自己问过的一个问题:我们是否应该完全放弃ListView,只使用RecyclerView? - eqtèöck
1
@OLeeCsobert 很好的问题。目前我在一些遗留代码中使用ListView,当我需要过滤器和SectionIndexers时,因为它们可以与ListView开箱即用(尽管它与可折叠应用栏不兼容)。在新项目或升级代码中,我专门使用RecyclerViews。 - Eugen Pechanec
1
我已经在使用Android 5.1.1的Nexus 4上进行了检查,但问题并没有得到解决。 - Samik Bandyopadhyay
我发现将它添加到父活动的XML中的片段布局中可以解决问题。我的ListView设置为匹配父级,这也可能是必要的。 - Christopher Mills

1

另一种方法是使用RecyclerView代替ListView,它肯定可行......

或者

使用:

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

"

它显然只能在棒棒糖上运行。

"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接