设置RecyclerView高度以匹配内容

时间:2015-08-18 11:54:02

标签: android layout material-design android-recyclerview

我的布局如下:

<?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保持“不可见”(例如,高度为零):

There should be news

如何动态调整RecyclerView的高度以匹配其内容的高度?

1 个答案:

答案 0 :(得分:-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();