@Override
public View getView(int position, View convertView, ViewGroup parent) {}
position: The position of the item within the adapter's data set of the item whose view we want.
parent: the parent that the view will eventually be attached to .
convertView: The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view. Heterogeneous(异构) lists can specify(指定) their number of view types, so that this View is always of the right type (seegetViewTypeCount()
andgetItemViewType(int)
).
convertView是一个View,不是一个类 ,不能灵活控制,主要是负责界面listview中Item的交替显示的,不在当前页面显示的item(old view)将被重新利用来显示即将出现的Item.这个convertView被reuse的缺点是:它是直接将上一个View拿来用,这时候你需要注意的是你的View是不是带状态的View,如果是的话,记得加上状态判断。
ViewHolder是一个类,里面可以放很多的View , 主要是负责对convertView界面显示的一些细节做一些调整(主要是通过控制viewHolder对象的成员变量设置来达到调整convertView的界面显示细节的,因为之前是有将convertView的一些子View传递给viewHolder,这个将子View传递给viewHolder的动作也带来了一个好处,如果你的view是通过布局文件布局的,那就可以节省好多个findViewById(int Rid)的开销,如果你是用代码创建的View,那就可以节省好多个View赋值的开销)ViewHolder有点像一个“代理”/“托儿所” , convertView将自己的孩子都交给他来管理和控制。convertView通过setTag();方法将ViewHolder布局保存下来,相当于是标记了自己的孩子,到时候领孩子的时候不至于领错了。如果需要丰富的布局方式,ViewHolder是必不可少的。
notice:View的setTag和getTag方法的参数是Object,也就是说你在setTag里面放数据做数据处理也是可以的。
ViewHolder的托管能力还是很强的,你可以把它当作自定义控件来操作。
综上:convertView节省的是创建listview新的Item的空间和时间开销,viewHolder节省的是item子View的初始化时间,同时让convertView界面控制更灵活、高效。
简单实例1:基础Demo实例
private class MyAdapter extends BaseAdapter{
private List<String> list;
public void setList(List list){
this.list = list;
}
public MyAdapter(){
}
public MyAdapter(List list){
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int arg0) {
return list.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
private int i = 0;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
android.util.Log.d("诡异的调用会出现吗?","false");
ViewHolder viewHolder = null ;
if(convertView == null){
convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.myadapteritem, null);
viewHolder = new ViewHolder();
viewHolder.textview = (TextView) convertView.findViewById(R.id.test_miss);
viewHolder.initViewHolder();
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textview.setText("miss : "+ i);
i++;
return convertView;
}
protected class ViewHolder{
private TextView textview;
private void initViewHolder(){
this.textview.setTextSize(20);
}
}
}
<span style="white-space:pre"> </span>listview = (ListView) findViewById(R.id.listView1) ;
adapter = new MyAdapter();
List list = new ArrayList<String>();
list.add("1");list.add("1");list.add("1");list.add("1");list.add("1");
list.add("1");list.add("1");list.add("1");list.add("1");list.add("1");
adapter.setList(list);
listview.setAdapter(adapter);
简单的实例2:强调托管
class BaseAdapterDialog extends BaseAdapter {
private ArrayList<HashMap<String, String>> mListHashmap;
public BaseAdapterDialog() {
mListHashmap = mList_DialogStringHashMapList.get(flag);
for(int pos =0;pos<mListHashmap.size();pos++){
mListHashmap.get(pos).put("isSelected", "false");
}
}
@Override
public int getCount() {
return mListHashmap.size();
}
@Override
public Object getItem(int position) {
return mListHashmap.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int pos = position;
Log.d("--zy--", "position: " + pos);
final ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(SearchActivity.this)
.inflate(R.layout.list_item_singaltxt, null);
viewHolder = new ViewHolder();
viewHolder.mCheckbox = (CheckBox) convertView
.findViewById(R.id.ItemCheckbox);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
((TextView) (convertView.findViewById(R.id.ItemTitle)))
.setText(mListHashmap.get(pos).get("title"));
// 多选的情况 添加多选的控件 选择状态保存 点击添加参数
if (flag == QIYEGUIMO || flag == GUIMOQINGKUANG
|| flag == HANGYEJIANGUAN) {
/*
* CheckBox checkbox = viewHolder.mCheckbox ; (CheckBox)
* convertView .findViewById(R.id.ItemCheckbox);
*/
viewHolder.mCheckbox.setVisibility(View.VISIBLE);
// Log.d("--zy--", "position: " + pos);
viewHolder.setPos(pos);
class listitemCheckboxOnCheckedChangeListener implements OnCheckedChangeListener {
private int pos;
public listitemCheckboxOnCheckedChangeListener(int pos) {
Log.d("--zy--", "init OnCheckedChangedListener pos: " + pos);
this.pos = pos;
}
public int getPos() {
return this.pos;
}
@Override
public void onCheckedChanged(CompoundButton arg0,
boolean arg1) {
Log.d("--zy--", "position Inner callback: " + this.pos);
viewHolder.setSelected(arg0.isChecked());
}
}
Log.d("--zy--",
"viewHolder pos: " + viewHolder.getPos());
viewHolder.mCheckbox.setOnCheckedChangeListener(new listitemCheckboxOnCheckedChangeListener(
viewHolder.getPos()));
viewHolder.mCheckbox.setChecked(viewHolder.getStatus());
}
return convertView;
}
class ViewHolder {
private int pos;
private boolean isChecked;
private CheckBox mCheckbox;
public void setSelected(boolean isChecked) {
this.isChecked = isChecked;
mListHashmap.get(pos)
.put("isSelected", "" + isChecked + "");
}
public boolean getStatus() {
Log.d("--zy--","ViewHolder getStatus(): "+mListHashmap.get(pos).get("isSelected"));
if (mListHashmap.get(pos).get("isSelected").equals("true")) {
return true;
} else {
return false;
}
}
public void setPos(int pos) {
this.pos = pos;
}
public int getPos() {
return this.pos;
}
}
}
通过这个例子会了解一些问题。
帮助资料1