//该适配器实现了ListView条目的复用以及控件的复用,不需要再每次getitem时候再找控件与布局
package ;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import ;
/**
* Created by Liz on 2016/5/6.
* 实现了listView的优化
*/
public abstract class MyBaseAdapter extends BaseAdapter {
//实现了listView的优化
public ViewHolder holder = null;
@Override
public abstract int getCount();
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return initView(position, convertView, parent);
}
//回调init、setLayoutId
public View initView(int position, View convertView, ViewGroup parent) {
int layoutId = setLayoutId();
if (convertView == null) {
convertView = View.inflate(BaseApplication.getInstance(), layoutId, null);
holder = new ViewHolder();
bindHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
// Log.e("```````","复用了");
}
return setWidget(position, convertView, parent);
}
//回调setHolder
public void bindHolder(View convertView) {
setHolder(convertView);//把getView的converView传递给Holder
}
//子类实现用以设置控件UI,返回convertView,返回值不能为空,为convertView
public abstract View setWidget(int position, View convertView, ViewGroup parent);
//子类找到自己的控件并设置给ViewHolder,convertView是getView传递下来的convertView,负责传参,用convertView.findViewById();
public abstract void setHolder(View convertView);
//返回Adapter所在的View的Id
public abstract int setLayoutId();
//添加自己的控件类型
public class ViewHolder {
public ImageView img_01;
public ImageView img_02;
public TextView tv_01;
public TextView tv_02;
public TextView tv_03;
public TextView tv_04;
public TextView tv_05;
public TextView tv_06;
public RadioButton rb_01;
}
}
//实例
class GetCouponAdapter extends MyBaseAdapter {
@Override
public int getCount() {
return coupon_infos.size();
}
@Override
public View setWidget(int position, View convertView, ViewGroup parent) {
holder.tv_01.setText(coupon_infos.get(position).getCup_amount());
holder.tv_02.setText(coupon_infos.get(position).getCup_name());
return convertView;
}
@Override
public void setHolder(View convertView) {
holder.tv_01 = (TextView) convertView.findViewById(R.id.m_count);
holder.tv_02 = (TextView) convertView.findViewById(R.id.goods_name;
}
@Override
public int setLayoutId() {
return R.layout.item_getcoupon;
}
}