【发布时间】:2015-11-11 08:21:38
【问题描述】:
我的布局是这样的:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent" android:layout_width="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Latest News:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="35sp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/news"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="20dip"
android:text="Something else"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="35sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo bar..."
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
</ScrollView>
然后我像这样向 RecyclerView 添加项目:
// download the feed...
RecyclerView rv = (RecyclerView) v.findViewById(R.id.news);
rv.setLayoutManager(new LinearLayoutManager(getActivity()));
rv.setAdapter(new NewsAdapter(feed.getItems()));
现在我希望 RecyclerView 能够自动调整自身大小以匹配其中项目的长度。但这并没有发生,而是 RecyclerView 保持“不可见”(例如高度为零):
如何动态调整 RecyclerView 的高度以匹配其内容的高度?
【问题讨论】:
-
你不能!这是 Android 的一个众所周知的限制。它不能很好地处理嵌套滚动。所以你不能在 ScrollView 中有 RecyclerView。
标签: android layout material-design android-recyclerview