BaseAdapter

packagecom.example.lenovo.myapplication.baseadapter;

importandroid.content.Context;

importandroid.view.View;

importandroid.view.ViewGroup;

importandroid.widget.BaseAdapter;

importjava.util.List;

/**

* Created by Lenovo on 2017/11/10.

*/

public abstract classCommonAdapterextendsBaseAdapter {

publicContextcontext;

publicListlistData;

private intlayoutId;

publicCommonAdapter(Context context, List listData,intlayoutId) {

super();

this.context= context;

this.listData= listData;

this.layoutId= layoutId;

}

@Override

public intgetCount() {

return this.listData==null?0:this.listData.size();

}

@Override

publicObject getItem(intposition) {

return this.listData.get(position);

}

@Override

public longgetItemId(intposition) {

returnposition;

}

@Override

publicView getView(intposition, View convertView, ViewGroup parent) {

CommonViewHolder holder = CommonViewHolder.getViewHolder(this.context, convertView, parent,this.layoutId);

this.fillData(holder, position);

returnholder.getMConvertView();

}

protected abstract voidfillData(CommonViewHolder holder,intposition);

}

====================================================================================================================================================

packagecom.example.lenovo.myapplication.baseadapter;

importandroid.content.Context;

importandroid.util.SparseArray;

importandroid.view.LayoutInflater;

importandroid.view.View;

importandroid.view.ViewGroup;

/**

* Created by Lenovo on 2017/11/10.

*/

public classCommonViewHolder {

privateSparseArraymViews;

privateViewmConvertView;

publicCommonViewHolder(Context context, ViewGroup parent,intlayoutId) {

mViews=newSparseArray<>();

mConvertView= LayoutInflater.from(context).inflate(layoutId, parent,false);

mConvertView.setTag(this);

}

public staticCommonViewHolder getViewHolder(Context context, View convertView, ViewGroup parent,intlayoutId) {

if(convertView ==null)

return newCommonViewHolder(context, parent, layoutId);

return(CommonViewHolder) convertView.getTag();

}

@SuppressWarnings("unchecked")

publicTgetView(intviewId) {

View item =mViews.get(viewId);

if(item ==null) {

item =mConvertView.findViewById(viewId);

mViews.put(viewId, item);

}

return(T) item;

}

publicView getMConvertView() {

returnmConvertView;

}

}

你可能感兴趣的:(BaseAdapter)