標籤:android
在項目開發的時候,相信大家可能會遇到一個ListView中出現多個不同的布局,遇到這個問題我的大致思路就是建立多個viewholder,在getViewType的時候設定不同位置的item用不同的viewholder,好了不廢話那麼多直接上代碼:
package com.sunny.youdao;import java.util.ArrayList;import java.util.List;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.CheckBox;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;public class MyAdapter extends BaseAdapter {private Context mContext;private LinearLayout linearLayout = null;private LayoutInflater inflater;private List<String> list = new ArrayList<String>();private TextView tex;private final int VIEW_TYPE = 3;private final int TYPE_1 = 0;private final int TYPE_2 = 1;private final int TYPE_3 = 2;public MyAdapter(Context context, List<String> list) {// TODO Auto-generated constructor stubthis.mContext = context;this.list = list;inflater = LayoutInflater.from(mContext);}@Overridepublic int getCount() {// TODO 自動產生的方法存根return list.size();}@Overridepublic Object getItem(int position) {// TODO 自動產生的方法存根return list.get(position);}@Overridepublic long getItemId(int position) {// TODO 自動產生的方法存根return position;}//每個convert view都會調用此方法,獲得當前所需要的view樣式 @Override public int getItemViewType(int position) { // TODO Auto-generated method stub int viewtype = position%6; if(viewtype == 0) return TYPE_1; else if(viewtype < 3) return TYPE_2; else if(viewtype < 6) return TYPE_3; else return TYPE_1; } //返回樣式的數量 @Override public int getViewTypeCount() { // TODO Auto-generated method stub return 3; }@Overridepublic View getView(int position, View convertView, ViewGroup parent) {viewHolder1 holder1 = null;viewHolder2 holder2 = null;viewHolder3 holder3 = null;int type = getItemViewType(position);// 無convertView,需要new出各個控制項if (convertView == null) {Log.e("convertView = ", "###convertView為空白###");// 按當前所需的樣式,確定new的布局switch (type) {case TYPE_1:convertView = inflater.inflate(R.layout.listitem1, parent,false);holder1 = new viewHolder1();holder1.textView = (TextView) convertView.findViewById(R.id.textview1);holder1.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);Log.e("convertView = ", "配置樣式一");convertView.setTag(holder1);break;case TYPE_2:convertView = inflater.inflate(R.layout.listitem2, parent,false);holder2 = new viewHolder2();holder2.textView = (TextView) convertView.findViewById(R.id.textview2);Log.e("convertView = ", "配置樣式二");convertView.setTag(holder2);break;case TYPE_3:convertView = inflater.inflate(R.layout.listitem3, parent,false);holder3 = new viewHolder3();holder3.textView = (TextView) convertView.findViewById(R.id.textview3);holder3.imageView = (ImageView) convertView.findViewById(R.id.imageview);Log.e("convertView = ", "配置樣式三");convertView.setTag(holder3);break;}} else {// 有convertView,按樣式,取得不用的布局switch (type) {case TYPE_1:holder1 = (viewHolder1) convertView.getTag();Log.e("convertView= ", "配置樣式一");break;case TYPE_2:holder2 = (viewHolder2) convertView.getTag();Log.e("convertView= ", "配置樣式二");break;case TYPE_3:holder3 = (viewHolder3) convertView.getTag();Log.e("convertView= ", "配置樣式三");break;}}// 設定資源switch (type) {case TYPE_1:holder1.textView.setText(Integer.toString(position));holder1.checkBox.setChecked(true);break;case TYPE_2:holder2.textView.setText(Integer.toString(position));break;case TYPE_3:holder3.textView.setText(Integer.toString(position));holder3.imageView.setBackgroundResource(R.drawable.icon);break;}return convertView;}// 各個布局的控制項資源class viewHolder1 {CheckBox checkBox;TextView textView;}class viewHolder2 {TextView textView;}class viewHolder3 {ImageView imageView;TextView textView;}}
代碼比較直觀明了,注釋也比較詳細,就不在詳細說明了,歡迎大家一塊學習交流~
本文出自 “sunnygeek技術部落格” 部落格,請務必保留此出處http://sunnygeek.blog.51cto.com/9485654/1671245
Android ListView存在多個item樣式的處理方法