BottomSheetDialogFragment 内部的 RecyclerView (wrap_content)
RecyclerView (wrap_content) inside of a BottomSheetDialogFragment
我遇到了一个棘手的问题,我不知道如何解决这个问题。
在我的项目中,我有一个自定义 BottomSheetDialogFragment
,在布局中有一个 FrameLayout
来添加或替换 Fragment
s.
现在我有一个 Fragment
,里面有一个 RecyclerView
和 height:="wrap_content"
,因为我希望 BottomSheetDialogFragment
只使用必要的 space。一切看起来都很好,当我在同一布局中放置另一个视图并设置该视图下方或上方的 RecyclerView
时出现问题。
RecyclerView
忽略其他视图(或多个视图)的大小并始终增长到最大屏幕大小,然后就不可能看到一些元素甚至滚动。
我看到一个solution,一些开发者建议添加paddingBottom
等于视图的高度。但在我的情况下不起作用,因为我想要一个动态解决方案。
上面我将分享一些问题图片和 GitHub Repository 示例。
感谢您的关注!
我已经设法完成您需要的工作,只需将其用作您的 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 上的重量会根据需要增加它的高度,直到填满屏幕。您添加的下一个项目将被添加到回收站视图中,但您需要滚动列表才能看到它
android 开发者博客说:-
The scrolling containers in your bottom sheet must support nested scrolling .
尝试如下更改 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 工具并接收其转换回调。
我遇到了一个棘手的问题,我不知道如何解决这个问题。
在我的项目中,我有一个自定义 BottomSheetDialogFragment
,在布局中有一个 FrameLayout
来添加或替换 Fragment
s.
现在我有一个 Fragment
,里面有一个 RecyclerView
和 height:="wrap_content"
,因为我希望 BottomSheetDialogFragment
只使用必要的 space。一切看起来都很好,当我在同一布局中放置另一个视图并设置该视图下方或上方的 RecyclerView
时出现问题。
RecyclerView
忽略其他视图(或多个视图)的大小并始终增长到最大屏幕大小,然后就不可能看到一些元素甚至滚动。
我看到一个solution,一些开发者建议添加paddingBottom
等于视图的高度。但在我的情况下不起作用,因为我想要一个动态解决方案。
上面我将分享一些问题图片和 GitHub Repository 示例。
我已经设法完成您需要的工作,只需将其用作您的 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 上的重量会根据需要增加它的高度,直到填满屏幕。您添加的下一个项目将被添加到回收站视图中,但您需要滚动列表才能看到它
android 开发者博客说:-
The scrolling containers in your bottom sheet must support nested scrolling .
尝试如下更改 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 工具并接收其转换回调。