【问题标题】:RecyclerView (wrap_content) inside of a BottomSheetDialogFragmentBottomSheetDialogFragment 内的 RecyclerView (wrap_content)
【发布时间】:2018-09-05 14:43:21
【问题描述】:

我在这里遇到了一个棘手的情况,我不知道如何解决这个问题。
在我的项目中,我有一个自定义 BottomSheetDialogFragment,在布局中使用 FrameLayout 来添加或替换 Fragments。

现在我有一个Fragment,在里面我有一个RecyclerViewheight:="wrap_content",因为我希望BottomSheetDialogFragment 只使用必要的空间。一切看起来都很好,当我在同一布局中放置另一个视图并将RecyclerView 设置在该视图的下方或上方时,就会出现问题。
RecyclerView 会忽略其他视图(或多个视图)的大小并始终增长到最大屏幕大小,然后就无法看到一些元素甚至滚动。

我看到一个solution,一些开发者建议添加paddingBottom等于视图的高度。但在我的情况下不起作用,因为我想要一个动态的解决方案。

上面我将分享一些问题的图片和GitHub Repository 的示例。

感谢您的关注!

【问题讨论】:

  • 嗯,这是一个典型的情况。。你所有的项目都是动态添加的吗?
  • 没有,但我有很多fragments,不同的布局和一些recycleviews
  • 据我了解,您需要先获取添加项目的父视图的区域..取决于您可以操作的区域。
  • 尝试使用 LinearLayout 而不是 FrameLayout!
  • @xbadal 我已经尝试过使用 LinearLayout 和 Constraint 没有成功

标签: android android-recyclerview bottom-sheet


【解决方案1】:

我已经完成了您需要的操作,只需将其用作您的 fragment_sample.xml

<LinearLayout 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="wrap_content"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rclItems"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>

<Button
    android:id="@+id/btnAddMoreItems"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rclItems"
    android:text="@string/add_1_item"/>

</LinearLayout>

解释 使用 LinearLayout 使您能够处理重量,垂直方向允许您将一个项目放置在另一个下方。 recyclerview 上的重量会根据需要增加它的高度,直到填满屏幕。您添加的下一个项目将被添加到 recyclerview,但您需要滚动列表才能看到它

【讨论】:

  • 是的,它工作正常,但我仍然感到困惑......你知道为什么以这种方式工作,而不是在 ReleativeLayoutConstraintLayout 内使用 wrap_content 吗?
  • 是的,因为在相对或约束下,按钮将始终位于回收站视图下方。因此,如果它有 100 条记录低于第 100 条将是按钮。我建议你的方式是强制按钮可见,并让回收站视图根据需要展开,直到达到屏幕高度。然后它仍然可以增长,但由于按钮始终可见,您将需要滚动回收器以查看它们。这就是权重的工作原理
  • 是的,很有意义@sebasira!谢谢你的解释!
  • @sebasira 我不知道为什么,但它完美地工作并解决了我们在BottomSheetDialogFragment 中遇到的RecyclerView 的所有问题。只有在 Android 上,你可以通过只替换父布局来解决一堆问题 :)
【解决方案2】:

android 开发者博客说:-

底部表单中的滚动容器必须支持嵌套滚动。

尝试如下更改您的fragment_sample.xml 以使recyclerview 滚动正常工作并使添加按钮保持不变。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 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="wrap_content">

 <android.support.v4.widget.NestedScrollView
 android:layout_width="match_parent"
 android:id="@+id/next"
 android:layout_above="@id/btnAddMoreItems"
 android:layout_height="wrap_content">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rclItems"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
</android.support.v4.widget.NestedScrollView>
   <Button
    android:id="@+id/btnAddMoreItems"
    android:layout_width="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_height="wrap_content"
    android:text="@string/add_1_item"/>
</RelativeLayout>

注意:将bottomsheet布局设置为CoordinatorLayout的子视图将允许您获取实现BottomSheetBehavior并接收其转换回调。

【讨论】:

  • 不要将 RecyclerView 放入滚动视图。 RecyclerView 支持嵌套滚动。 The android developers blog says 始终发布链接以支持您的声明。
猜你喜欢
  • 2015-05-06
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 2016-07-01
  • 1970-01-01
相关资源
最近更新 更多