Android ListViewに複数のitemスタイルが存在する処理方法
4521 ワード
プロジェクト開発の時、みんなが1つのListViewの中で複数の異なるレイアウトに出会うかもしれないと信じています.この問題に遭遇した私の大まかな考え方は複数のviewholderを作成し、getView Typeの時に異なる位置の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 stub
this.mContext = context;
this.list = list;
inflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
// TODO
return list.size();
}
@Override
public Object getItem(int position) {
// TODO
return list.get(position);
}
@Override
public 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;
}
@Override
public 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;
}
}
コードは比较的に直観的に明らかで、注釈も比较的に详しくて、详しく说明していないで、みんなを歓迎していっしょに交流を学びます~