以下代码为适用于ListView的Adapter的部分代码,请根据代码解释...
以下代码为适用于ListView的Adapter的部分代码,请根据代码解释下
1)getView方法中对convertView是否为空做了不同处理,目的是什么?
2)viewholder有什么作用?
private List<String> mData = new ArrayList<>()
…
private static class ViewHolder {
TextView text
public ViewHolder(View rootView) {
text = (TextView)rootView.findViewById(R.id.text)
}
public void bind(String item) {
...
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String item = mData.get(position)
ViewHolder viewHolder
if (null == convertView) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false)
viewHolder = new ViewHolder(convertView)
convertView.setTag(viewHolder)
} else {
viewHolder = (ViewHolder) convertView.getTag()
}
viewHolder.bind(item)
return convertView
}