【问题标题】:Set RecyclerView height to match content设置 RecyclerView 高度以匹配内容
【发布时间】: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


【解决方案1】:

你可以使用这样的代码:

rv.post(new Runnable() {
    @Override
    public void run() {
        final int newHeight = // number of items * one item height in px
        ViewGroup.LayoutParams params = rv.getLayoutParams();

        if (params == null) {
            params = ((ViewGroup)rv.getParent()).generateDefaultLayoutParams();
            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        }

        params.height = newHeight;
        rv.setLayoutParams(params);
    }
}

你可以使用这个技巧:

View view = getLayoutInflater().inflate(R.layout.*your_item_id*, null, false);
ItemViewHolder holder = new ItemViewHolder(view);
//set data to this holder, same code as onBindViewHolder(...)

view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.AT_MOST);
int itemHeight = view.getMeasuredHeight();

【讨论】:

  • 是的,这可以工作(但是generateDefaultLayoutParams 具有受保护的访问权限)。但是我如何确定一个项目在显示之前的高度?
猜你喜欢
  • 2013-08-20
  • 2019-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-21
  • 2015-04-07
  • 1970-01-01
  • 2017-02-01
相关资源
最近更新 更多