package com.example.androidtestproject.listview.moretype; import java.util.List; import com.example.androidtestproject.R; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class MoreLayoutAdapter extends BaseAdapter { private List<Person> list; private Context context; private final int TYPE_AT = 0; private final int TYPE_POST_REPLY = 1; private final int TYPE_POST_SYSTEM = 2; private LayoutInflater inflater; public MoreLayoutAdapter(Context context, List<Person> list) { this.context = context; this.list = list; } [@Override](https://my.oschina.net/u/1162528) public int getCount() { return list.size(); } [@Override](https://my.oschina.net/u/1162528) public Person getItem(int position) { return list.get(position); } [@Override](https://my.oschina.net/u/1162528) public long getItemId(int position) { return position; } [@Override](https://my.oschina.net/u/1162528) public View getView(int position, View convertView, ViewGroup parent) { AtHolder atHolder = null; PostReplayHolder postReplayHolder = null; PostSystemHolder postSystemHolder = null; int type = getItemViewType(position); if (convertView == null) { inflater = LayoutInflater.from(context); switch (type) { case TYPE_AT: convertView = inflater.inflate(R.layout.more_layout1, null); atHolder = new AtHolder(); atHolder.textView = (TextView) convertView.findViewById(R.id.text1); convertView.setTag(atHolder); break; case TYPE_POST_REPLY: convertView = inflater.inflate(R.layout.more_layout2, null); postReplayHolder = new PostReplayHolder(); postReplayHolder.button = (Button) convertView.findViewById(R.id.button1); convertView.setTag(postReplayHolder); break; case TYPE_POST_SYSTEM: convertView = inflater.inflate(R.layout.more_layout3, null); postSystemHolder = new PostSystemHolder(); postSystemHolder.imageView = (ImageView) convertView.findViewById(R.id.image1); convertView.setTag(postSystemHolder); break; default: break; } } else { switch (type) { case TYPE_AT: atHolder = (AtHolder) convertView.getTag(); break; case TYPE_POST_REPLY: postReplayHolder = (PostReplayHolder) convertView.getTag(); break; case TYPE_POST_SYSTEM: postSystemHolder = (PostSystemHolder) convertView.getTag(); break; default: break; } } // set content switch (type) { case TYPE_AT: atHolder.textView.setText(list.get(position).getName()); break; case TYPE_POST_REPLY: postReplayHolder.button.setText(list.get(position).getName()); break; case TYPE_POST_SYSTEM: postSystemHolder.imageView.setBackgroundResource(R.drawable.ic_launcher); break; default: break; } return convertView; } [@Override](https://my.oschina.net/u/1162528) public int getItemViewType(int position) { if (list.get(position).getType() == 0) { return TYPE_AT; } else if (list.get(position).getType() == 1) { return TYPE_POST_REPLY; } else if (list.get(position).getType() == 2) { return TYPE_POST_SYSTEM; } return super.getItemViewType(position); } @Override public int getViewTypeCount() { return 4; } public class AtHolder { TextView textView; } public class PostReplayHolder { Button button; } public class PostSystemHolder { ImageView imageView; } public class SystemFirendHolder { } public class SystemOtherHolder { }
}java