public abstract class AbsBaseAdapter<t> extends BaseAdapter {
private final int resId;
private List<t> list;
private Context context;
private ViewHolder viewHolder;
public AbsBaseAdapter(Context context,int resId) {
this.context = context;
list = new ArrayList<t>();
this.resId = resId;
}
public void setData(List<t deep="10"> list){
this.list = list;
notifyDataSetChanged();
}
public void addData(T data){
if(!list.contains(data)){
list.add(data);
notifyDataSetChanged();
}
}
public void clearData(){
list.clear();
notifyDataSetChanged();
}
public void remove(int poaition){
list.remove(poaition);
notifyDataSetChanged();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
View view;
if( convertView== null){
view = LayoutInflater.from(context).inflate(resId,viewGroup,false);
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
}else{
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
bindData(list.get(i),viewHolder);
return view;
}
public abstract void bindData(T data,ViewHolder viewHolder);
class ViewHolder{
View layoutView;
Map<integer,view> map = new HashMap<integer,view>();
public ViewHolder(View layoutView) {
this.layoutView = layoutView;
}
public View getView(int id){
View view;
if(map.containsKey(id)){
view = map.get(id);
}else{
view = layoutView.findViewById(id);
map.put(id,view);
}
return view;
}
}</integer,view></integer,view></t></t></t></t>
|