Android - 如何动态更改Recyclerview的高度?

21

我遇到了一个问题,需要根据Recycler的总项目更改其高度。我尝试使用如下的Layout Param:

        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);

但是它没起作用。我该怎么办?请帮帮我!


是的,我忘了注意到那个问题。原因是我想在ScrollView中同时使用多个RecyclerView。如果我将任何一个RecyclerView的高度设置为wrapcontent,它将占用所有空间,但是当我设置固定高度(例如100dp)时,它可以正常工作。因此,我正在寻找一种动态更改RecyclerView高度的方法。 - Lạng Hoàng
事实上,我的应用程序有一个表单,允许用户输入数据,并且他可以通过点击“+”按钮添加尽可能多的新表单。因此,我正在尝试这种方式:当他点击“+”按钮时,我将添加一个项目到recyclerview,以便我可以通过recyclerview控制一切。目前为止,为了节省时间,我选择了另一种方法来完成,但仍然想知道如何通过这种方式来完成。也许有很多人也想知道。 - Lạng Hoàng
https://viksaaskool.wordpress.com/2015/05/08/adjust-recyclerview-item-height-example/ - Pratik Butani
我之前也遇到了同样的问题,后来发现高度是以像素为单位设置的。如果要以dp为单位设置,则需要将高度转换为dp - (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, heightInPixels, getResources().getDisplayMetrics()); - kushpf
5个回答

14

在调用setLayoutParams(params)方法时,应该使用父视图的LayoutParams。

例如:

<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。

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

7
我刚把LinearLayout改成了RelativeLayout,然后它就自动调整位置了。 - Sai

5

我试过这个方法,它有效。也许会有所帮助。

@Override
public void onBindViewHolder(FeedListRowHolder feedListRowHolder, int i) {

    //this change height of rcv
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
     params.height =80; //height recycleviewer
     feedListRowHolder.itemView.setLayoutParams(params);

    FeedItem feedItem = feedItemList.get(i);

    Picasso.with(mContext).load(feedItem.getThumbnail())
            .error(R.drawable.placeholder)
            .placeholder(R.drawable.placeholder)
            .into(feedListRowHolder.thumbnail);

    feedListRowHolder.title.setText(Html.fromHtml(feedItem.getTitle()));
    feedListRowHolder.itemView.setActivated(selectedItems.get(i, false));

    feedListRowHolder.setClickListener(new FeedListRowHolder.ClickListener() {
        public void onClick(View v, int pos, boolean isLongClick) {
            if (isLongClick) {

                // View v at position pos is long-clicked.
                String poslx = pos + "";
                Toast.makeText(mContext, "longclick " + poslx, Toast.LENGTH_SHORT).show();

            } else {
                // View v at position pos is clicked.
                String possx = pos + "";
                Toast.makeText(mContext, "shortclick " + possx, Toast.LENGTH_SHORT).show();
                toggleSelection(pos);
            }
        }
    });

}

看一下你的代码,似乎你试图更改回收视图中每个项目的高度。但我想改变的是回收视图本身的高度。 - Lạng Hoàng
当你知道项目数量时,你可以设置RecycleView的高度,例如:params.height = itemHeight * numberOfItem; 但是你可能想要固定每个项目的高度和RecycleView的高度。 - eurosecom

3

如果你只想让你的RecyclerView根据项目数量自动调整大小,那么为什么不将RecyclerView高度设置为wrap_content。

如果您的布局中有多个ScrollView,请尝试将RecyclerView包装在NestScrollView中,并将其高度也设置为wrap_content。

代码:

<android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />

2
尽管这个问题已经问了相当长的一段时间,但我认为有些人可能会发现这个答案有帮助。
我有一个带有适配器的RecyclerView。高度在适配器的onBindViewHolder方法中设置:
布局:
  <android.support.v7.widget.RecyclerView
  android:id="@+id/container"
  ...
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>

Adapter:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

public abstract static class ViewHolder extends RecyclerView.ViewHolder {
     public ViewHolder(View itemView) {
        super(itemView);
    }
    public abstract void setFixedHeight();
}

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);

    return new ViewHolder(view) {

        @Override
        public void setFixedHeight() {
            //  magic happening here
            ViewGroup.LayoutParams parentParams = parent.getLayoutParams();
            parentParams.height =
                ((RecyclerView) parent).computeVerticalScrollRange()
                    + parent.getPaddingTop()
                    + parent.getPaddingBottom();
            parent.setLayoutParams(parentParams);
        }
    }; 
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.setFixedHeight();
    }

// other methods here
}

设置适配器:

recyclerView.setAdapter(new MyAdapter(...));

注意:在水平滚动时使用((RecyclerView) parent).computeHorizontalScrollRange()


-2

这段代码可行,我对此很确定

ViewGroup.LayoutParams params=recyclerview.getLayoutParams();
params.height=100;
recyclerview.setLayoutParams(params);

另外一件你可以做的事情是将线性布局作为回收视图的父级,然后动态增加父视图的高度。
考虑下面的XML代码:
<LinearLayout 
      android:id="@+id/yourLayoutId">

    <RecyclerView android:width="match_parent" android:height="wrap_content">
    </RecyclerView>

</LinearLayout

请考虑以下代码

LinearLayout layout = (LinearLayout)findViewById(R.id.yourLayoutId);
LinearLayout.LayoutParams lp = (LayoutParams) layout.getLayoutParams();
lp.height = 100;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接