问题描述
我有一个列表视图定制ArrayAdapter。我有两个类型的pretty多,除了像背景和路线的一些东西图形相同内容的行。
I have a custom ArrayAdapter for a listview. I have two types of rows with pretty much the same content except some graphical things like background and alignments.
这是什么样子:
public class ChatAdapter extends ArrayAdapter<MessageSource.Message> {
private Context context;
private List<MessageSource.Message> items;
private int deviceId;
public ChatAdapter(Context context, int layoutResourceId, List<MessageSource.Message> items, int deviceId) {
super(context, layoutResourceId, items);
this.context = context;
this.items = items;
this.deviceId = deviceId;
}
static class ViewHolder {
protected TextView from;
protected TextView message;
protected TextView time;
}
@Override
public View getView(int p, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolder viewHolder ;
if(convertView == null) {
if (items.get(p).deviceId == deviceId) {
convertView = inflater.inflate(R.layout.chat_row_right, parent, false);
} else {
convertView = inflater.inflate(R.layout.chat_row_left, parent, false);
}
viewHolder = new ViewHolder();
viewHolder.from = (TextView) convertView.findViewById(R.id.bubble_from);
viewHolder.message = (TextView) convertView.findViewById(R.id.bubble_message);
viewHolder.time = (TextView) convertView.findViewById(R.id.bubble_time);
viewHolder.from.setTypeface(Font.rLight(context));
viewHolder.message.setTypeface(Font.rLight(context));
viewHolder.time.setTypeface(Font.rLight(context));
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.from.setText(items.get(p).nickName);
viewHolder.message.setText(items.get(p).message);
viewHolder.time.setText(DateUtils.getRelativeTimeSpanString(items.get(p).created * 1000, System.currentTimeMillis(), 0));
return convertView;
}
}
通过这条线我决定使用哪个布局的:
With this line I determine which of the layouts to use:
if (items.get(p).deviceId == deviceId) {
但它似乎是随机忽略,选择布局,因为它看到适合。
我做错了什么在这里的任何想法?
But it seems to be randomly ignored and chooses the layout as it see fits.Any ideas of what I'm doing wrong here?
推荐答案
您正在检查的DeviceID只有当convertView == NULL。为了得到正确的结果,你也应该检查什么布局convertView为您提供的包含,如果它是不正确类型的充气一个新的。
You are checking deviceId only when convertView == null. To get correct result you should also check what layout convertView offered to you contains and if it is not of correct type inflate a new one.
幸运的ListView有像你这样的情况下支持 - 覆盖getViewTypeCount和getItemViewType如下:
Fortunately listview has support for situations like yours - override getViewTypeCount and getItemViewType as follows:
@Override
public int getItemViewType( int p ) {
return items.get(p).deviceId == deviceId ? 0 : 1;
}
@Override
public int getViewTypeCount() {
return 2;
}
然后,你就不必检查视图类型,因为列表视图处理这张支票给你 - 它缓存您单独使用全部视图类型,并将正确的您getView方法
Then you do not have to check View type because listview handles this check for you - it caches all view types you use separately and passes correct one to your getView method.
这篇关于Android的ArrayAdapter每一次viewholder给予不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!