我想向用户展示一个可滚动的项目列表,每个条目可以有不同数量的“子视图”。例如:

我的第一个想法是创建一个RecyclerView,它将膨胀每个包含自己的RecyclerView的“条目”:
项目:
<LinearLayout
android:id="@+id/item">
<TextView
android:id="@+id/itemTitle" />
<android.support.v7.widget.RecyclerView
android:id="@+id/nestedRecyclerView"/>
</LinearLayout>我遇到的问题是,主RecyclerView不知道嵌套列表的大小,因此只显示其上方的TextView,因为它认为嵌套列表的高度为零。
通过将固定的布局:高度设置为嵌套列表,可以在某种程度上固定,但如示例图片的第3项所示,高度可能会根据项目的数量而变化。
这是创建这样一个布局的首选方法吗?如果是,我如何解决“高度”问题?如果没有,最好的方法是什么?
发布于 2015-12-17 05:02:15
可以通过添加列表适配器布局的网格视图来实现。
您需要像下面这样对网格视图进行子类化,这样可以适当地测量孩子的身高。
public class NonScrollableGridView extends GridView {
public NonScrollableGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Do not use the highest two bits of Integer.MAX_VALUE because they are
// reserved for the MeasureSpec mode
int heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightSpec);
getLayoutParams().height = getMeasuredHeight();
}}
https://stackoverflow.com/questions/34326698
复制相似问题