gpt4 book ai didi

java - 如果我使用不同的 ViewItemType,我应该使用 viewHolders 吗?

转载 作者:行者123 更新时间:2023-11-29 08:54:29 25 4
gpt4 key购买 nike

我最初的问题发布在这里:

ListView for messaging app shows wrong listItem layout after scrolling

但是我有点困惑,因为有两个答案被赞成,我想同时使用它们。首先,我认为我应该使用 getItemViewType 方法来帮助我提高性能,这是一个安全的假设。不过在那之后,我是否仍应使用 Google 文档中关于 Making ListView Scrolling Smooth 的 viewHolder 模式? ?

如果我确实使用 ViewHolder 代码,我是否将其合并到 getView 中?

static class ViewHolder {
TextView text;
TextView timestamp;
ImageView icon;
ProgressBar progress;
int position;
}

static public enum LAYOUT_TYPE {
INBOUND,
OUTBOUND
}

@Override
public int getViewTypeCount () {
return LAYOUT_TYPE.values().length;
}

@Override
public int getItemViewType (int position) {
if ( messages.get(position).isOutbound())
return LAYOUT_TYPE.OUTBOUND.ordinal();
else
return LAYOUT_TYPE.INBOUND.ordinal();
}

@Override
public View getView (int position, View convertView, ViewGroup parent) {
LAYOUT_TYPE itemType = LAYOUT_TYPE.values()[getItemViewType(position)];
... (code until inflater )
switch (itemType){
case INBOUND:
convertview = /inflate & configure inbound layout
ViewHolder holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.listitem_image);
holder.text = (TextView) convertView.findViewById(R.id.listitem_text);
holder.timestamp = (TextView) convertView.findViewById(R.id.listitem_timestamp);
holder.progress = (ProgressBar) convertView.findViewById(R.id.progress_spinner);
convertView.setTag(holder);
break;
case OUTBOUND:
convertview = /inflate & configure outbound layout
ViewHolder holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.listitem_image);
holder.text = (TextView) convertView.findViewById(R.id.listitem_text);
holder.timestamp = (TextView) convertView.findViewById(R.id.listitem_timestamp);
holder.progress = (ProgressBar) convertView.findViewById(R.id.progress_spinner);
convertView.setTag(holder);
break;
}

最佳答案

//典型的getView

public View getView(int position, View containerRow, ViewGroup parent) {

if (containerRow == null) {

//One time inflate
containerRow = inflater.inflate(R.layout.yourLayout, parent, false);

//instantiate all view from layout in levent relevent object.

//One time instantiation of viewholder
viewHolder = new ViewHolder();

viewHolder.someview = viewFromLayou;

//finally
containerRow.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) containerRow.getTag();
}
}

//你的情况应该是

public View getView(int position, View containerRow, ViewGroup parent) {

if (containerRow == null) {

//One time inflate
containerRow = inflater.inflate(R.layout.yourLayout, parent, false);

//instantiate all view from layout in levent relevent object.

//One time instantiation of viewholder
if(inbound)
inBoundViewHolder = new ViewHolder();
else
outBoundViewHolder = new ViewHolder();

viewHolder.someview = viewFromLayou;

//finally
containerRow.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) containerRow.getTag();
}
}

关于java - 如果我使用不同的 ViewItemType,我应该使用 viewHolders 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20981634/

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