不多说,先上图。![]()
1.DataModel.java
这个主要是用来存放数据模型的类。即在列表项中存在不同的view的时候,可以这样使用。
package com.sunplusedu.customadapger;
public class DataModel {
private String name;
private int drawableId;
private int type;
public DataModel(String name, int drawableId, int type) {
this.name = name;
this.drawableId = drawableId;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDrawableId() {
return drawableId;
}
public void setDrawableId(int drawableId) {
this.drawableId = drawableId;
}
public int getType(){
return type;
}
}
2.MyAdapter.java
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.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter{
public final static int TYPE_BUTTON = 0;
public final static int TYPE_CHECKBOX = 1;
private final static int TYPE_NUM = 2;
private final List<DataModel> mList;
private LayoutInflater mInflater;
public MyAdapter(Context context, List<DataModel> list) {
this.mInflater = LayoutInflater.from(context);
this.mList = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
DataModel d = mList.get(position);
Log.i("getItemViewType()","OK");
return d.getType();
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return TYPE_NUM;
}
//用来保存view的引用
final class ViewHolder{
public TextView textView;
public ImageView imageView;
public Button button;
public CheckBox checkBox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
DataModel model= mList.get(position);
Log.d("MyAdataer", "in get view positon = " + position);
ViewHolder holder = null;
if (convertView == null) {
Log.d("MyAdataer", "a new View");
holder=new ViewHolder();
switch(model.getType()){
case MyAdapter.TYPE_BUTTON:
convertView = mInflater.inflate(R.layout.button_item, null);
holder.imageView = (ImageView)convertView.findViewById(R.id.imageView);
holder.textView = (TextView)convertView.findViewById(R.id.textView);
holder.button = (Button)convertView.findViewById(R.id.button);
//可以在此添加事件处理等
break;
case MyAdapter.TYPE_CHECKBOX:
convertView = mInflater.inflate(R.layout.check_item, null);
holder.imageView = (ImageView)convertView.findViewById(R.id.imageView);
holder.textView = (TextView)convertView.findViewById(R.id.textView);
holder.checkBox = (CheckBox)convertView.findViewById(R.id.checkBox);
break;
}
convertView.setTag(holder);
}
//刷新数据
switch(model.getType()){
case MyAdapter.TYPE_BUTTON:
holder = (ViewHolder) convertView.getTag();
holder.imageView.setBackgroundResource(model.getDrawableId());
holder.textView.setText(model.getName());
break;
case MyAdapter.TYPE_CHECKBOX:
holder = (ViewHolder) convertView.getTag();
holder.imageView.setBackgroundResource(model.getDrawableId());
holder.textView.setText(model.getName());
break;
}
return convertView;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
3.CustomAdapterActivity.java
package com.sunplusedu.customadapger;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
public class CustomAdapterActivity extends ListActivity {
/** Called when the activity is first created. */
private List<DataModel> mData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mData = getData();
MyAdapter adapter = new MyAdapter(this, mData);
setListAdapter(adapter);
}
private List<DataModel> getData() {
List<DataModel> list = new ArrayList<DataModel>();
list.add(new DataModel("Click ", R.drawable.head1, MyAdapter.TYPE_BUTTON));
list.add(new DataModel("Check", R.drawable.head1, MyAdapter.TYPE_CHECKBOX));
list.add(new DataModel("AAA ", R.drawable.head2, MyAdapter.TYPE_CHECKBOX));
list.add(new DataModel("BBB ", R.drawable.head2, MyAdapter.TYPE_BUTTON));
list.add(new DataModel("CCC ", R.drawable.head2, MyAdapter.TYPE_BUTTON));
list.add(new DataModel("DDD ", R.drawable.head2, MyAdapter.TYPE_CHECKBOX));
list.add(new DataModel("EEE ", R.drawable.head1, MyAdapter.TYPE_CHECKBOX));
list.add(new DataModel("FFF ", R.drawable.head1, MyAdapter.TYPE_BUTTON));
list.add(new DataModel("GGG ", R.drawable.head1, MyAdapter.TYPE_CHECKBOX));
list.add(new DataModel("HHH ", R.drawable.head3, MyAdapter.TYPE_CHECKBOX));
list.add(new DataModel("1111 ", R.drawable.head1, MyAdapter.TYPE_BUTTON));
list.add(new DataModel("2222 ", R.drawable.head3, MyAdapter.TYPE_CHECKBOX));
list.add(new DataModel("3333", R.drawable.head3, MyAdapter.TYPE_CHECKBOX));
return list;
}
}
package com.sunplusedu.customadapger;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
public class CustomAdapterActivity extends ListActivity {
/** Called when the activity is first created. */
private List<DataModel> mData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mData = getData();
MyAdapter adapter = new MyAdapter(this, mData);
setListAdapter(adapter);
}
private List<DataModel> getData() {
List<DataModel> list = new ArrayList<DataModel>();
list.add(new DataModel("Click ", R.drawable.head1, MyAdapter.TYPE_BUTTON));
list.add(new DataModel("Check", R.drawable.head1, MyAdapter.TYPE_CHECKBOX));
list.add(new DataModel("AAA ", R.drawable.head2, MyAdapter.TYPE_CHECKBOX));
list.add(new DataModel("BBB ", R.drawable.head2, MyAdapter.TYPE_BUTTON));
list.add(new DataModel("CCC ", R.drawable.head2, MyAdapter.TYPE_BUTTON));
list.add(new DataModel("DDD ", R.drawable.head2, MyAdapter.TYPE_CHECKBOX));
list.add(new DataModel("EEE ", R.drawable.head1, MyAdapter.TYPE_CHECKBOX));
list.add(new DataModel("FFF ", R.drawable.head1, MyAdapter.TYPE_BUTTON));
list.add(new DataModel("GGG ", R.drawable.head1, MyAdapter.TYPE_CHECKBOX));
list.add(new DataModel("HHH ", R.drawable.head3, MyAdapter.TYPE_CHECKBOX));
list.add(new DataModel("1111 ", R.drawable.head1, MyAdapter.TYPE_BUTTON));
list.add(new DataModel("2222 ", R.drawable.head3, MyAdapter.TYPE_CHECKBOX));
list.add(new DataModel("3333", R.drawable.head3, MyAdapter.TYPE_CHECKBOX));
return list;
}
}