BaseAdapter<T> 重写 createViewFromResource实现界面,刷新,加载,移除

  1   import java.util.ArrayList;
  2 import java.util.List;
  4 import android.content.Context;
  5 import android.view.LayoutInflater;
  6 import android.view.View;
  7 import android.view.ViewGroup;
  9 import com.lidroid.xutils.util.LogUtils;
 10 
 11 /**
 12  * 基本的适配器,重写 createViewFromResource实现界面
 13  * 
 14  * @author HZY
 15  *
 16  * @param <T>
 17  *            list存放的实体
 18  */
 19 public class BaseAdapter<T> extends android.widget.BaseAdapter {
 20     private Context mContext;
 21     private LayoutInflater mInflater;
 22     private List<T> mDatas = new ArrayList<T>();
 23 
 24     public BaseAdapter(Context mContext, List<T> mDatas) {
 25         this.mContext = mContext;
 26         this.mDatas = mDatas;
 27         this.mInflater = LayoutInflater.from(mContext);
 28     }
 29 
 30     public Context getContext() {
 31         return this.mContext;
 32     }
 33 
 34     public LayoutInflater getLayoutInflater() {
 35         return this.mInflater;
 36     }
 37 
 38     /**
 39      * 刷新 41      * @param mDatas
 42      */
 43     public void refresh(List<T> mDatas) {
 44         if (mDatas != null) {
 45             LogUtils.d("size=>" + mDatas.size());
 46         }
 47         this.mDatas = mDatas;
 48         notifyDataSetChanged();
 49     }
 50 
 51     /**
 52      * 加载更多 54      * @param mDatas
 55      */
 56     public void loadMore(List<T> mDatas) {
 57         if (mDatas == null) {
 58             this.mDatas = mDatas;
 59         } else {
 60             this.mDatas.addAll(mDatas);
 61         }
 62 
 63         notifyDataSetChanged();
 64     }
 65 
 66     /**
 67      * 移除 69      * @param date
 70      */
 71     public void remove(T date) {
 72         if (mDatas != null) {
 73             mDatas.remove(date);
 74             notifyDataSetChanged();
 75         }
 77     }
 78 
 79     /**
 80      * 移除 82      * @param position
 83      */
 84     public void remove(int position) {
 85         if (mDatas != null && mDatas.size() > position) {
 86             mDatas.remove(position);
 87             notifyDataSetChanged();
 88         }
 89     }
 90 
 91     @Override
 92     public int getCount() {
 93         return mDatas == null ? 0 : mDatas.size();
 94     }
 95 
 96     @Override
 97     public Object getItem(int position) {
 98         return mDatas == null ? null : mDatas.get(position);
 99     }
100 
101     @Override
102     public long getItemId(int position) {
103         return position;
104     }
105 
106     @Override
107     public View getView(int position, View convertView, ViewGroup parent) {
108         return createViewFromResource(position, convertView, parent);
109     }
110 
111     protected View createViewFromResource(int position, View convertView,
112             ViewGroup parent) {
113         // TODO Auto-generated method stub
114         return null;
115     }
116 
117 }

 

posted on 2016-03-15 15:49  大米稀饭  阅读(457)  评论(0编辑  收藏  举报