gpt4 book ai didi

android - 更新后如何将 RecyclerView ViewHolder 更改回原始布局

转载 作者:行者123 更新时间:2023-11-29 17:31:29 24 4
gpt4 key购买 nike

我有一个用于 RelativeLayoutRecyclerView ViewHolder。在我的 OnBindViewHolder 方法中,我正在根据条件更新整个布局和包含的 ImageView 的高度。这工作正常,但是,这个新布局正在被回收用于不符合条件的后续 View 。因此,产生不一致的结果。

// Involves populating data into the item through holder
@Override
public void onBindViewHolder(RallyAdapter.ViewHolder viewHolder, final int position) {

//Expand viewholder and thumbnail if rally name takes up multiple lines
if(rally.getName().length() > 23) {

RelativeLayout.LayoutParams relParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

//Original height is 114
relParams.height = 139

//where 'relativeLayout' is referencing the base layout
viewHolder.relativeLayout.setLayoutParams(relParams);

RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//Original height is 117
imgParams.height = 143

viewHolder.thumbnail.setLayoutParams(imgParams);
} else {

//Need code here to reset layout and thumbnail to original heights
}
}

我知道我的条件必须有一个 else,因为我在 onBindViewHolder 函数中,但我只是不知道它应该是什么。

最佳答案

如果我要写这个。我可能会有两个不同高度的两个不同的 xml 文件。然后我可以在适配器中使用 getItemViewType() 来判断哪个位置属于哪种类型。通过这种方式,回收器 View 会为您所在的每个位置回收正确的 View 。(您不需要重新设置每一行的大小)

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

private final static int TYPE_1=1;
private final static int TYPE_2=2;
@Override
public int getItemViewType(int position) {
if(rally.getName().length() > 23) {
return TYPE_1;
}
return TYPE_2;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) {
View view = LayoutInflater.from(mContext).inflate(/** your layout **/)
if (i == TYPE_1) {
RelativeLayout.LayoutParams relParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 139);
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 143);
viewHolder.relativeLayout.setLayoutParams(relParams);
viewHolder.thumbnail.setLayoutParams(imgParams);
return new RecyclerView.ViewHolder(/**View_height_139**/);
}
return new RecyclerView.ViewHolder(/**View_height_114**/);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
if (viewHolder.getItemViewType() == TYPE_1) {
// Do the binding for when rally.getName().length > 23
} else {
// Do the bindings for the rest
}
}

@Override
public int getItemCount() {
return 10.;
}
}

关于android - 更新后如何将 RecyclerView ViewHolder 更改回原始布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32703615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com