Android ListView不同的item布局实现

时间:2021-07-17 18:05:32

Android ListView不同的item布局实现

类似与这个 聊天效果 和 进入直播间


注意 在使用不同布局的时候,在适配器中 getItemViewType和getViewTypeCount不能少,一般的同一种item的列表是不用这两个函数的重载的,但是如果需要不同种类的item,就必须重载这两个


下面来看具体例子


public class VideoMessageAdapter extends BaseAdapter {

private final ForegroundColorSpan redSpan;
private final ForegroundColorSpan graySpan;
private final ForegroundColorSpan yeSpan;
private Context mContext;
private ViewHolder1 holder1;
private ViewHolder2 holder2;
private List<String> data;
private String name;
private final int TYPE_1 = 0;
private final int TYPE_2 = 1;
private int index; //名字的长度,用于字体着色

public VideoMessageAdapter(Context context, List<String> data) {
this.mContext = context;
this.data = data;
//ForegroundColorSpan 为文字前景色,BackgroundColorSpan为文字背景色
redSpan = new ForegroundColorSpan(context.getResources().getColor(R.color.video_text_color_red));
graySpan = new ForegroundColorSpan(context.getResources().getColor(R.color.video_text_color_green));
yeSpan = new ForegroundColorSpan(context.getResources().getColor(R.color.luxury_gold));
}

@Override
public int getCount() {
return data == null ? 0 : data.size();
}

@Override
public Object getItem(int position) {
return data.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

public void NotifyAdapter(List<String> data,int index) {
this.data = data;
this.index = index;
notifyDataSetChanged();
}

@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
if (position == data.size() - 1)
return TYPE_2;
else
return TYPE_1;
}

@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 2;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
holder1 = null;
holder2 = null;
int type = getItemViewType(position);
if (convertView == null) {

switch (type) {
case TYPE_1:
holder1 = new ViewHolder1();
convertView = View.inflate(mContext, R.layout.item_messageadapter, null);
holder1.tvcontent1 = (TextView) convertView.findViewById(R.id.tvcontent);
convertView.setTag(holder1);
break;
case TYPE_2:
holder2 = new ViewHolder2();
convertView = View.inflate(mContext, R.layout.item_messageadapter1, null);
holder2.tvcontent2 = (TextView) convertView.findViewById(R.id.tvcontent1);
convertView.setTag(holder2);
break;
}
} else {
switch (type) {
case TYPE_1:
holder1 = (ViewHolder1) convertView.getTag();

break;
case TYPE_2:
holder2 = (ViewHolder2) convertView.getTag();

break;
}
}
switch (type) {
case TYPE_1:
holder1.tvcontent1.setText(data.get(position));
if (position % 2 == 0) {
//这里注意一定要先给textview赋值
SpannableStringBuilder builder = new SpannableStringBuilder(holder1.tvcontent1.getText().toString());
//为不同位置字符串设置不同颜色
builder.setSpan(graySpan, 0, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//最后为textview赋值
holder1.tvcontent1.setText(builder);
} else if (position % 3 == 1) {
//这里注意一定要先给textview赋值
SpannableStringBuilder builder = new SpannableStringBuilder(holder1.tvcontent1.getText().toString());
//为不同位置字符串设置不同颜色
builder.setSpan(redSpan, 0, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//最后为textview赋值
holder1.tvcontent1.setText(builder);
} else {
//这里注意一定要先给textview赋值
SpannableStringBuilder builder = new SpannableStringBuilder(holder1.tvcontent1.getText().toString());
//为不同位置字符串设置不同颜色
builder.setSpan(graySpan, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//最后为textview赋值
holder1.tvcontent1.setText(builder);
}
break;
case TYPE_2:
holder2.tvcontent2.setText(data.get(position));
//这里注意一定要先给textview赋值
SpannableStringBuilder builder = new SpannableStringBuilder(holder2.tvcontent2.getText().toString());
//为不同位置字符串设置不同颜色
builder.setSpan(yeSpan, 0, index, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//最后为textview赋值
holder2.tvcontent2.setText(builder);
break;
}
return convertView;
}

private final class ViewHolder1 {
TextView tvcontent1;
}

private final class ViewHolder2 {
TextView tvcontent2;
}
}
大概就到这了  祝大家编辑代码愉快!