java如何限制NestedScrollView中RecyclerView的高度
在我当前的Android应用程序中,我有一个屏幕显示安卓.support.v4.app.DialogFragment
此DialogFragment
视图包含以下UI组件
HEADING
== Sub Heading
== NestedScrollView
==== RecyclerView
==== RadioGroup
==== Spinner
==== EditText
==== Action Buttons
DialogFragment
配置为全屏,使用如下样式:-
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.AppDialogTheme);
}
我的对话风格是
<!-- Define your custom dialog theme here extending from base -->
<style name="AppDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<!-- Define color properties as desired -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">#000</item>
<item name="安卓:textColorHighlight">@color/background_url</item>
<item name="colorAccent">@color/dark_grey</item>
<item name="colorControlNormal">@color/colorPrimaryDark</item>
<!-- Define window properties as desired -->
<item name="安卓:windowNoTitle">true</item>
<item name="安卓:windowFullscreen">true</item>
<item name="安卓:windowBackground">@安卓:color/white</item>
<item name="安卓:windowIsFloating">true</item>
<item name="安卓:windowCloseOnTouchOutside">false</item>
</style>
我使用NestedScrollView
的原因是View
可以在纵向和横向模式下工作
我希望限制height
的RecyclerView
我得到的最接近的是使用下面的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="vertical">
<TextView
安卓:id="@+id/headline_literal"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="10dp"
安卓:gravity="center"
安卓:padding="10dp"
安卓:text="Heading"
安卓:textSize="20sp"
安卓:textStyle="bold" />
<View
安卓:id="@+id/divider"
安卓:layout_width="fill_parent"
安卓:layout_height="2dp"
安卓:layout_marginTop="5dp"
安卓:background="#c0c0c0" />
<安卓.support.v4.widget.NestedScrollView
安卓:layout_width="match_parent"
安卓:layout_height="0dp"
安卓:layout_weight="1"
tools:context=".MainActivity">
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:weightSum="5"
安卓:orientation="vertical">
<TextView
安卓:id="@+id/sub_headline_literal"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:gravity="center"
安卓:padding="10dp"
安卓:text="Some long texts having a long size so that it takes multiple lines in the view to replicate the real-life app use case. This is important to have 3-4 lines this textview so that we can see if the views are being populated correctly. Hope this sentence is long enough to replicate the real-life scenario of this TextView content. Thank you."
安卓:textSize="16sp"
安卓:textStyle="normal" />
<安卓.support.v7.widget.RecyclerView
安卓:id="@+id/dummy_rv"
安卓:layout_width="match_parent"
安卓:layout_height="0dp"
安卓:layout_margin="10dp"
安卓:layout_marginStart="9dp"
安卓:layout_marginEnd="9dp"
安卓:layout_weight="1"
安卓:background="@drawable/rv_border"
安卓:fadingEdge="horizontal"
安卓:fadingEdgeLength="10dp"
安卓:padding="10dp"
安卓:requiresFadingEdge="vertical" />
<RadioGroup
安卓:id="@+id/myRadioGroup"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_gravity="center"
安卓:layout_marginTop="10dp"
安卓:checkedButton="@+id/sound">
<RadioButton
安卓:id="@+id/sound"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Sound" />
<RadioButton
安卓:id="@+id/vibration"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Vibration" />
<RadioButton
安卓:id="@+id/silent"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Silent" />
</RadioGroup>
<EditText
安卓:id="@+id/notes"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:hint="Notes" />
<LinearLayout
安卓:id="@+id/buttons"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal"
安卓:padding="10dp">
<TextView
安卓:id="@+id/cancel_button"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:gravity="center"
安卓:text="Cancel" />
<TextView
安卓:id="@+id/submit_button"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:gravity="center"
安卓:text="Submit" />
</LinearLayout>
</LinearLayout>
</安卓.support.v4.widget.NestedScrollView>
</LinearLayout>
通过在NestedScrollView
的内部LinearLayout
上使用weightSum
,我可以限制Recyclerview
的高度。但是NestedScrollView
高度太大,超过一半的高度为空白
我如何限制我的RecyclerView
的高度并使NestedScrollView
达到wrap_content
我试过NestedScrollView
用身高wrap_content
但没有效果
如何实现所需的UI?提前谢谢
# 1 楼答案
如果还需要通过定制NestedScrollView来限制NestedScrollView的大小:
attr。xml
自定义嵌套滚动视图的布局示例:
与此定制的NestedScrollView一起,如果您应用RecyclerView的定制,那么它将完全按照您想要的方式工作。我希望这有帮助
# 2 楼答案
我建议在中添加页脚和页眉
RecyclerView
中添加一个页眉和页脚,而不是一个NestedRecyclerView
,这将很好地将整个内容放置在我所看到的布局中。我想给你提供一个到my answer here的链接,在那里你可以找到如何在^{因此,我建议创建一个带有
headline_literal
和divider
的视图,并将其用作页眉,而RadioGroup
、EditText
和Button
将位于页脚。如果你有任何问题,请告诉我我已经尝试自己实现您想要的行为,并告诉我以下实现是否适合您。我也在Github中添加了这个
让我们首先声明一个适配器,用于向
RecyclerView
添加页眉和页脚现在让我们逐一定义布局。这里是
list_item_normal.xml
而
list_item_footer.xml
应该如下所示最后,
list_item_header.xml
应该有以下内容现在,您已经将原始布局的组件划分为多个部分。因此,主布局应该如下所示
因此,我将分享一个示例
Activity
来运行这段代码,它将显示整体实现希望有帮助
# 3 楼答案
自定义回收器视图以设置
maxHeight
n attrs。xml
在xml中设置RecyclerView height
wrap_content
,在dp中设置maxHeight为fixwidth。 RecyclerView将使用height wrap_内容,直到您设置的fixWidth,在达到maxHeight后,RecyclerView将可滚动