Mactivity
public class MainActivity extends AppCompatActivity{
ListView listView;
ArrayList<bean> list=new ArrayList<>();
Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.listview);
list.add(new bean("张三","18","男"));
adapter=new Adapter<bean>(this,list,R.layout.bj){
@Override
protected void convert(ViewHolder helper, bean item, int position) {
helper.setText(R.id.name,item.getNamne());
helper.setText(R.id.age,item.getAge());
helper.setText(R.id.sex,item.getSex());
//条目点击事件
//helper.getConvertView().setOnClickListener
//普通点击事件
// helper.getView(R.id.check).setOnClickListener
}
};
listView.setAdapter(adapter);
}
}
_______________________________________________________________________________
Adapter
public abstract class Adapter<T> extends BaseAdapter {
Context Context;
List<T> list;
int item;
public Adapter(android.content.Context context, List<T> list, int item) {
Context = context;
this.list = list;
this.item = item;
}
@Override
public int getCount() {
if(list==null)
return 0;
return list.size();
}
@Override
public T getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder= ViewHolder.get(Context,convertView,parent,item);
convert(viewHolder,getItem(position),position);
return viewHolder.getConvertView();
}
protected abstract void convert(ViewHolder helper,T item,int position);
}
___________________________________________________________________________________________________
ViewHolder
public class ViewHolder {
private SparseArray<View> mViews = new SparseArray<>();
private View mConvertView;
private Context context;
private ViewHolder(Context context, ViewGroup parent, int layoutId) {
this.context = context;
mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
mConvertView.setTag(this);
}
static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId) {
if (convertView == null)
return new ViewHolder(context, parent, layoutId);
return (ViewHolder) convertView.getTag();
}
public View getConvertView() { return mConvertView; }
/**
* 通过 ID 获取控件
*/
public <T extends View> T getView(int viewId) {
View view = mViews.get(viewId);
if (view == null) {
view = mConvertView.findViewById(viewId);
mViews.put(viewId, view);
}
return (T) view;
}
/**
* 设置 text
*/
public ViewHolder setText(int viewId, String text) {
TextView view = getView(viewId);
if (view == null) return this;
view.setText(text);
return this;
}
/**
* 设置文本颜色
*/
public void setTextColor(int viewId, int colorId) {
TextView view = getView(viewId);
if (view == null) return;
view.setTextColor(ContextCompat.getColor(context, colorId));
}
/**
* 获取 text
*/
public String getTextStr(int viewId) {
TextView view = getView(viewId);
if (view == null) return null;
return view.getText().toString();
}
/**
* 设置资源图片
*/
public ViewHolder setImg(int viewId, int drawableId) {
ImageView iv = getView(viewId);
iv.setImageResource(drawableId);
return this;
}
/**
* 设置 Bitmap
*/
public ViewHolder setImg(int viewId, Bitmap bitmap) {
ImageView iv = getView(viewId);
iv.setImageBitmap(bitmap);
return this;
}
}