Android-如何动态更改Recyclerview的高度? [英] Android - how to change Recyclerview height dynamically?

查看:623
本文介绍了Android-如何动态更改Recyclerview的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个有关根据回收站总项目更改回收站高度的问题. 我尝试过的是使用Layout Param这样的:

I'm stuck with an issue about changing Recycler height based on its total items. What I have tried is to use Layout Param like this:

        ViewGroup.LayoutParams params = myRecyclerView.getLayoutParams();
        params.height = itemHeight * numberOfItem;
        myRecyclerView.requestLayout();

       ViewGroup.LayoutParams params = new  RecyclerView.LayoutParams(..WRAP_CONTENT, ...WRAP_CONTENT);;
       params.height = itemHeight * numberOfItem;
       myRecyclerView..setLayoutParams(params);

但是没有用. 我该怎么做 ?请帮助我!

But it didn't work. How can I do it ? Please help me !

推荐答案

您应在setLayoutParams(params)中使用父级视图的LayoutParams.

You should use LayoutParams of parent's view in setLayoutParams(params).

例如:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <android.support.v7.widget.RecyclerView
        android:id="@+id/images"
        android:layout_width="wrap_content"
        android:layout_height="360dp"
        >
    </android.support.v7.widget.RecyclerView>
 </Relativelayout>

更改代码中的LayoutParams.

Change LayoutParams in the code.

  RelativeLayout.LayoutParams lp =
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 500);
 recyclerView.setLayoutParams(lp);

这篇关于Android-如何动态更改Recyclerview的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆