RecycledView(wrap_content)在BottomSheetDialogFragment中

ext*_*mkv 13 android android-recyclerview bottom-sheet

我在这里面临一个棘手的局面,我不知道如何解决这个问题.
在我的项目中,我有一个自定义BottomSheetDialogFragment,在布局FrameLayout中添加或替换Fragments.

现在我有一个Fragment和里面我有一个RecyclerViewheight:="wrap_content"因为我希望BottomSheetDialogFragment只使用必要的空间.一切看起来都很棒,当我将另一个视图放在同一个布局中并设置该RecyclerView视图的下方或上方时,问题就出现了.
RecyclerView忽略其他视图(或视图)的大小,总是长到最大屏幕尺寸,然后它是没有可能看到几个元素,甚至滚动.

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

上面我将分享一些问题的图像和GitHub存储库的样本.

在此输入图像描述 在此输入图像描述 感谢您的关注!

seb*_*ira 7

我已经设法做您需要做的,只需要使用它作为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>
Run Code Online (Sandbox Code Playgroud)

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

  • 是的,它正在工作,但我仍然感到困惑......你知道为什么以这种方式工作而不是在`ReleativeLayout`或`ConstraintLayout`中使用`wrap_content`吗? (2认同)