getView方法的抽取
getView方法中还有很多重复的部分
可以进行抽取
那么我们先分析一下getView方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.item_main_lv, null);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tv.setText(mList[position]);
return convertView;
}
ViewHolder中
static class ViewHolder{
View view;
ImageView iv;
TextView tv;
ViewHolder(View view) {
this.view = view;
iv=(ImageView) view.findViewById(R.id.iv_item_main_lv_icon);
tv=(TextView) view.findViewById(R.id.tv_item_main_lv_title);
}
}
我们来看看总共的步骤
1.创建ViewHolder
2.inflate对应的item的xml
3.然后我们把convertView传入了ViewHolder
4.然后ViewHolder中,就找到控件,然后赋值ViewHolder中的iv和tv
5.然后把ViewHolder设置给convertView,,,setTag
然后是else中
1.convertView获取ViewHolder,,,getTag
然后是获取item对应的数据
并且填充到ViewHolder中对应的控件中
最后返回了convertView,也就是条目
抽到ViewHolder里面去
2.3.4.5这几步的事情,其实可以全部放到ViewHolder里面去
怎么弄呢,看看代码
我们先不抽基类了
我们就先把getView中的代码搞到ViewHolder里面去
这里其实不是很难
好好看好好分析
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
viewHolder = new ViewHolder(parent.getContext(), convertView, R.layout.item_main_lv);
convertView = viewHolder.getConvertView();
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.initData(mList,position);
return convertView;
}
static class ViewHolder{
View mConvertView;
ImageView iv;
TextView tv;
ViewHolder(Context context,View view,int layoutId) {
view=View.inflate(context, layoutId, null);
iv=(ImageView) view.findViewById(R.id.iv_item_main_lv_icon);
tv=(TextView) view.findViewById(R.id.tv_item_main_lv_title);
view.setTag(this);
mConvertView=view;
}
public View getConvertView() {
return mConvertView;
}
public void initData(String[] list, int position) {
tv.setText(list[position]);
}
}
我们把getView中的能搞的都搞到ViewHolder里面去了
然后我们再抽ViewHolder
根据我们用过的ViewHolder就很好抽了
public abstract class BaseViewHolder {
public View mConvertView;
public BaseViewHolder(Context context, View view, int layoutId) {
view = View.inflate(context, layoutId, null);
initView(context, view, layoutId);
view.setTag(this);
mConvertView=view;
}
public abstract void initView(Context context, View view, int layoutId);
public View getConvertView() {
return mConvertView;
}
public abstract void initData(String[] list, int position);
}
抽完之后ViewHolder就更简单了
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
viewHolder = new ViewHolder(parent.getContext(), convertView, R.layout.item_main_lv);
convertView = viewHolder.getConvertView();
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.initData(mList, position);
return convertView;
}
static class ViewHolder extends BaseViewHolder {
ImageView iv;
TextView tv;
public ViewHolder(Context context, View view, int layoutId) {
super(context, view, layoutId);
}
@Override
public void initView(Context context, View view, int layoutId) {
iv = (ImageView) view.findViewById(R.id.iv_item_main_lv_icon);
tv = (TextView) view.findViewById(R.id.tv_item_main_lv_title);
}
@Override
public void initData(String[] list, int position) {
tv.setText(list[position]);
}
}
现在我们只要在initView方法里面去初始化控件
然后在initData里面初始化数据
就行了
就这么简单!
吊吊吊!!!