【问题标题】:View invisibility when RecyclerView is emptyRecyclerView 为空时查看不可见
【发布时间】:2016-03-12 18:17:14
【问题描述】:

我使用的是LinearLayout,里面有一些视图。倒数第二个是RecyclerView,最后一个是与RelativeLayout 链接的<include> 标签。我希望最后一个视图永久可见,因为我想使用它将项目添加到 RecyclerView

我的问题是,只要适配器为空,RecyclerView 下方的 <include> 视图就会消失。有什么办法可以避免这种行为?

编辑

我希望RecyclerView 和“添加”View 一起工作的方式存在限制。我希望“添加”视图始终是 RecyclerView 中的最后一项,就像在 Google Keep 列表中一样。

示例 Google Keep list example(我还不能添加图片)

【问题讨论】:

    标签: android android-layout android-recyclerview


    【解决方案1】:

    您可以尝试将 LinearLayout 替换为 RelativeLayout;然后将底部视图锚定到屏幕底部,并且 RecyclerView 将设置在该视图上方。像这样的东西(只是一个骨架):

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent>
    
        <-! other views here -->
    
        <include layout="@layout/something"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:id="@bottomView" />
    
        <RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/some_view_above_recyclerview"
            android:layout_above="@+id/bottomView" />
    
    </RelativeLayout>
    

    【讨论】:

    • 有了这个解决方案,我可以看到“”,没错。问题是我有另一个限制,我没有在我的问题上描述过分简化。我也会编辑我的答案。
    • 在这种情况下,您需要为适配器定义 2 种视图类型,最后一个条目将是视图类型“页脚”,您可以在其中添加页脚视图。但是,只有当您将列表滚动到底部时,此页脚才会可见。
    • 我已经这样做了,但我认为这个解决方案没有优化,因为对于一个非常小的问题,复杂性增加了太多。我在这里有另一个问题 (stackoverflow.com/questions/35927007/…) 关于如何在适配器中实现更改,以防您在那里查看并帮助我。可能我解决的方式太乱了,有更简单的方法。
    猜你喜欢
    • 1970-01-01
    • 2021-07-06
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多