這篇文章的效果也是你們常見的,各類通信應用的對話列表都是這種方式,像微信、whatsapp、易信、米聊等。咱們這篇文章也權當爲回憶,造成簡單的筆記。這篇文章參考了2009年Google IO中的《TurboChargeYourUI-How to make your AndroidUI fast and efficient》和2010年Google IO中的《The World of List View》。像2009年Google IO的資料仍是很前沿的,那會android開發資料不多,最重要的就是參考google發佈的各類資料。java
在《TurboChargeYourUI-How to make your AndroidUI fast and efficient》介紹了怎樣提升listview的性能,優化了listview的加載速度。這裏的item使用的是單一佈局,可以實現view的重用和回收,那麼多種佈局文件的怎麼辦呢,若是再使用上面的方法,view的重用會出現問題,Android使用的BaseAdapter提供瞭解決多種佈局文件的重用方法。android
1)重寫 getViewTypeCount() – 該方法返回多少個不一樣的佈局微信
2)重寫 getItemViewType(int) – 根據position返回相應的Itemapp
-
-
-
-
- public class ChatAdapter extends BaseAdapter {
-
- public static final String KEY = "key";
- public static final String VALUE = "value";
-
- public static final int VALUE_TIME_TIP = 0;
- public static final int VALUE_LEFT_TEXT = 1;
- public static final int VALUE_LEFT_IMAGE = 2;
- public static final int VALUE_LEFT_AUDIO = 3;
- public static final int VALUE_RIGHT_TEXT = 4;
- public static final int VALUE_RIGHT_IMAGE = 5;
- public static final int VALUE_RIGHT_AUDIO = 6;
- private LayoutInflater mInflater;
-
- private List<Message> myList;
-
- public ChatAdapter(Context context, List<Message> myList) {
- this.myList = myList;
-
- mInflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- }
-
- @Override
- public int getCount() {
- return myList.size();
- }
-
- @Override
- public Object getItem(int arg0) {
- return myList.get(arg0);
- }
-
- @Override
- public long getItemId(int arg0) {
- return arg0;
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup arg2) {
-
- Message msg = myList.get(position);
- int type = getItemViewType(position);
- ViewHolderTime holderTime = null;
- ViewHolderRightText holderRightText = null;
- ViewHolderRightImg holderRightImg = null;
- ViewHolderRightAudio holderRightAudio = null;
- ViewHolderLeftText holderLeftText = null;
- ViewHolderLeftImg holderLeftImg = null;
- ViewHolderLeftAudio holderLeftAudio = null;
-
- if (convertView == null) {
- switch (type) {
- case VALUE_TIME_TIP:
- holderTime = new ViewHolderTime();
- convertView = mInflater.inflate(R.layout.list_item_time_tip,
- null);
- holderTime.tvTimeTip = (TextView) convertView
- .findViewById(R.id.tv_time_tip);
- holderTime.tvTimeTip.setText(msg.getValue());
- convertView.setTag(holderTime);
- break;
-
- case VALUE_LEFT_TEXT:
- holderLeftText = new ViewHolderLeftText();
- convertView = mInflater.inflate(R.layout.list_item_left_text,
- null);
- holderLeftText.ivLeftIcon = (ImageView) convertView
- .findViewById(R.id.iv_icon);
- holderLeftText.btnLeftText = (Button) convertView
- .findViewById(R.id.btn_left_text);
- holderLeftText.btnLeftText.setText(msg.getValue());
- convertView.setTag(holderLeftText);
- break;
-
- case VALUE_LEFT_IMAGE:
- holderLeftImg = new ViewHolderLeftImg();
- convertView = mInflater.inflate(R.layout.list_item_left_iamge,
- null);
- holderLeftImg.ivLeftIcon = (ImageView) convertView
- .findViewById(R.id.iv_icon);
- holderLeftImg.ivLeftImage = (ImageView) convertView
- .findViewById(R.id.iv_left_image);
- holderLeftImg.ivLeftImage.setImageResource(R.drawable.test);
- convertView.setTag(holderLeftImg);
- break;
-
- case VALUE_LEFT_AUDIO:
- holderLeftAudio = new ViewHolderLeftAudio();
- convertView = mInflater.inflate(R.layout.list_item_left_audio,
- null);
- holderLeftAudio.ivLeftIcon = (ImageView) convertView
- .findViewById(R.id.iv_icon);
- holderLeftAudio.btnLeftAudio = (Button) convertView
- .findViewById(R.id.btn_left_audio);
- holderLeftAudio.tvLeftAudioTime = (TextView) convertView
- .findViewById(R.id.tv_left_audio_time);
- holderLeftAudio.tvLeftAudioTime.setText(msg.getValue());
- convertView.setTag(holderLeftAudio);
- break;
-
- case VALUE_RIGHT_TEXT:
- holderRightText= new ViewHolderRightText();
- convertView = mInflater.inflate(R.layout.list_item_right_text,
- null);
- holderRightText.ivRightIcon = (ImageView) convertView
- .findViewById(R.id.iv_icon);
- holderRightText.btnRightText = (Button) convertView
- .findViewById(R.id.btn_right_text);
- holderRightText.btnRightText.setText(msg.getValue());
- convertView.setTag(holderRightText);
- break;
-
- case VALUE_RIGHT_IMAGE:
- holderRightImg= new ViewHolderRightImg();
- convertView = mInflater.inflate(R.layout.list_item_right_iamge,
- null);
- holderRightImg.ivRightIcon = (ImageView) convertView
- .findViewById(R.id.iv_icon);
- holderRightImg.ivRightImage = (ImageView) convertView
- .findViewById(R.id.iv_right_image);
- holderRightImg.ivRightImage.setImageResource(R.drawable.test);
- convertView.setTag(holderRightImg);
- break;
-
- case VALUE_RIGHT_AUDIO:
- holderRightAudio=new ViewHolderRightAudio();
- convertView = mInflater.inflate(R.layout.list_item_right_audio,
- null);
- holderRightAudio.ivRightIcon = (ImageView) convertView
- .findViewById(R.id.iv_icon);
- holderRightAudio.btnRightAudio = (Button) convertView
- .findViewById(R.id.btn_right_audio);
- holderRightAudio.tvRightAudioTime = (TextView) convertView
- .findViewById(R.id.tv_right_audio_time);
- holderRightAudio.tvRightAudioTime.setText(msg.getValue());
- convertView.setTag(holderRightAudio);
- break;
-
- default:
- break;
- }
-
- } else {
- Log.d("baseAdapter", "Adapter_:"+(convertView == null) );
- switch (type) {
- case VALUE_TIME_TIP:
- holderTime=(ViewHolderTime)convertView.getTag();
- holderTime.tvTimeTip.setText(msg.getValue());
- break;
- case VALUE_LEFT_TEXT:
- holderLeftText=(ViewHolderLeftText)convertView.getTag();
- holderLeftText.btnLeftText.setText(msg.getValue());
- break;
- case VALUE_LEFT_IMAGE:
- holderLeftImg=(ViewHolderLeftImg)convertView.getTag();
- holderLeftImg.ivLeftImage.setImageResource(R.drawable.test);
- break;
- case VALUE_LEFT_AUDIO:
- holderLeftAudio=(ViewHolderLeftAudio)convertView.getTag();
- holderLeftAudio.tvLeftAudioTime.setText(msg.getValue());
- break;
- case VALUE_RIGHT_TEXT:
- holderRightText=(ViewHolderRightText)convertView.getTag();
- holderRightText.btnRightText.setText(msg.getValue());
- break;
- case VALUE_RIGHT_IMAGE:
- holderRightImg=(ViewHolderRightImg)convertView.getTag();
- holderRightImg.ivRightImage.setImageResource(R.drawable.test);
- break;
- case VALUE_RIGHT_AUDIO:
- holderRightAudio=(ViewHolderRightAudio)convertView.getTag();
- holderRightAudio.tvRightAudioTime.setText(msg.getValue());
- break;
-
- default:
- break;
- }
-
-
- }
- return convertView;
- }
-
-
-
-
-
-
-
- @Override
- public int getItemViewType(int position) {
-
- Message msg = myList.get(position);
- int type = msg.getType();
- Log.e("TYPE:", "" + type);
- return type;
- }
-
-
-
-
-
- @Override
- public int getViewTypeCount() {
- return 7;
- }
-
- class ViewHolderTime {
- private TextView tvTimeTip;
- }
-
- class ViewHolderRightText {
- private ImageView ivRightIcon;
- private Button btnRightText;
- }
-
- class ViewHolderRightImg {
- private ImageView ivRightIcon;
- private ImageView ivRightImage;
- }
-
- class ViewHolderRightAudio {
- private ImageView ivRightIcon;
- private Button btnRightAudio;
- private TextView tvRightAudioTime;
- }
-
- class ViewHolderLeftText {
- private ImageView ivLeftIcon;
- private Button btnLeftText;
- }
-
- class ViewHolderLeftImg {
- private ImageView ivLeftIcon;
- private ImageView ivLeftImage;
- }
-
- class ViewHolderLeftAudio {
- private ImageView ivLeftIcon;
- private Button btnLeftAudio;
- private TextView tvLeftAudioTime;
- }
-
- }
分享兩張微信、易信的圖,你也能夠作成這樣子。ide
Demo下載,請猛擊。佈局
* iOS入門羣:83702688
* android開發進階羣:241395671
*/