android 动画改变高度,Android – 如何动态更改Recyclerview高度?

您应该在setLayoutParams(params)中使用父视图的LayoutParams。

例如:

在代码中更改LayoutParams。

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

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

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

如果您只是希望您的回收站视图根据项目数自动resize,那么为什么不将RecyclerView高度设置为wrap_content。

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

代码:

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

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

布局:

适配器:

public class MyAdapter extends RecyclerView.Adapter{ 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()和水平滚动

这段代码可以肯定

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

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

请考虑以下XML

请考虑以下代码

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值