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

时间:2015-07-14 11:57:18

标签: android android-recyclerview

我遇到了一个关于根据总物品更改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);

但它没有用。 我该怎么做 ?请帮帮我!

5 个答案:

答案 0 :(得分:9)

你应该在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);

答案 1 :(得分:4)

我试过这个。有效。可能会有所帮助。

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

}

答案 2 :(得分:0)

如果您只是希望您的回收商视图根据商品数量自动调整大小,那么为什么不将RecyclerView高度作为wrap_content。

如果你在布局中有多个ScrollView,那么尝试在NestScrollView中包装RecyclerView并将其高度设置为wrap_content。

代码:

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

答案 3 :(得分:0)

虽然很久以前就提出了这个问题,但我想有些人可能会觉得这个答案很有帮助。

我有一个带适配器的RecyclerView。高度在适配器的onBindViewHolder方法中设置:

布局:

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

适配器:

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()与水平滚动

一起使用

答案 4 :(得分:-2)

此代码可以正常使用

Native-Activity Application

您可以做的另一件事是将线性布局作为回收者视图的父级,然后为父视图动态增加高度

考虑以下XML

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

考虑以下代码

<LinearLayout 
      andorid:id=""@+id/yourLayoutId"">

    <RecyclerView/>

</LinearLayout