BaseAdapter封装优化
来源:互联网 发布:plc编程培训 编辑:程序博客网 时间:2024/06/01 23:14
参照ArrayAdapter的源码,对BasAdapter进行封装。添加addAll(),remove(),clear(),sort()等操作数据源的方法。
代码如下
public abstract class BaseAdapterWraper<D> extends BaseAdapter { private List<D> mInfos = new ArrayList<>(); private final Object mLock = new Object(); protected LayoutInflater mInflater; protected Context context; private boolean mNotifyOnChange = true; public BaseAdapterWraper(Context context) { mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.context = context; } @Override public void notifyDataSetChanged() { super.notifyDataSetChanged(); mNotifyOnChange = true; } public void setNotifyOnChange(boolean notifyOnChange) { mNotifyOnChange = notifyOnChange; } public Context getContext() { return context; } public int getCount() { return isEmpty() ? 0 : mInfos.size(); } public D getItem(int position) { return isEmpty() ? null : mInfos.get(position); } public long getItemId(int position) { return position; } public abstract View getView(int position, View convertView, ViewGroup parent); public void setDatas(@Nullable List<D> infos) { mInfos = infos; notifyDataSetChanged(); } public List<D> getDatas() { return mInfos; } public boolean isEmpty() { return mInfos == null; } public void add(D t) { synchronized (mLock) { if (isEmpty()) { return; } mInfos.add(t); } if (mNotifyOnChange) notifyDataSetChanged(); } public void addAll(List<D> list) { synchronized (mLock) { if (isEmpty()) { return; } if (list != null) { mInfos.addAll(list); } } if (mNotifyOnChange) notifyDataSetChanged(); } public void clear() { if (isEmpty()) { return; } synchronized (mLock) { mInfos.clear(); } if (mNotifyOnChange) notifyDataSetChanged(); } public void remove(D t){ synchronized (mLock){ if(isEmpty()){ return; } if(mInfos != null){ mInfos.remove(t); } } if (mNotifyOnChange) notifyDataSetChanged(); } public void insert(D t, int index){ synchronized (mLock){ if(isEmpty()){ return; } if(mInfos != null){ mInfos.add(index,t); } } if (mNotifyOnChange) notifyDataSetChanged(); } public void sort(Comparator<? super D> comparator){ synchronized (mLock){ if(isEmpty()){ return; } if(mInfos != null){ Collections.sort(mInfos, comparator); } } if (mNotifyOnChange) notifyDataSetChanged(); } public void hideLastPositionView(int position, View v) { v.setVisibility(position == getCount() - 1 ? View.GONE : View.VISIBLE); } public void lastPositionDividerFull(int position, View v, int margin) { LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, DensityUtil.dip2px(context, 0.6f)); if (position == getCount() - 1) { params.setMargins(0, 0, 0, 0); } else { params.setMargins(margin, 0, 0, 0); } v.setLayoutParams(params); } @SuppressWarnings("unchecked") protected static <T extends View> T findViewById(View view, int id) { return (T) view.findViewById(id); }}
ArrayAdapter源码如下
public abstract class BaseAdapterWraper<D> extends BaseAdapter { private List<D> mInfos = new ArrayList<>(); private final Object mLock = new Object(); protected LayoutInflater mInflater; protected Context context; private boolean mNotifyOnChange = true; public BaseAdapterWraper(Context context) { mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.context = context; } @Override public void notifyDataSetChanged() { super.notifyDataSetChanged(); mNotifyOnChange = true; } public void setNotifyOnChange(boolean notifyOnChange) { mNotifyOnChange = notifyOnChange; } public Context getContext() { return context; } public int getCount() { return isEmpty() ? 0 : mInfos.size(); } public D getItem(int position) { return isEmpty() ? null : mInfos.get(position); } public long getItemId(int position) { return position; } public abstract View getView(int position, View convertView, ViewGroup parent); public void setDatas(@Nullable List<D> infos) { mInfos = infos; notifyDataSetChanged(); } public List<D> getDatas() { return mInfos; } public boolean isEmpty() { return mInfos == null; } public void add(D t) { synchronized (mLock) { if (isEmpty()) { return; } mInfos.add(t); } if (mNotifyOnChange) notifyDataSetChanged(); } public void addAll(List<D> list) { synchronized (mLock) { if (isEmpty()) { return; } if (list != null) { mInfos.addAll(list); } } if (mNotifyOnChange) notifyDataSetChanged(); } public void clear() { if (isEmpty()) { return; } synchronized (mLock) { mInfos.clear(); } if (mNotifyOnChange) notifyDataSetChanged(); } public void remove(D t){ synchronized (mLock){ if(isEmpty()){ return; } if(mInfos != null){ mInfos.remove(t); } } if (mNotifyOnChange) notifyDataSetChanged(); } public void insert(D t, int index){ synchronized (mLock){ if(isEmpty()){ return; } if(mInfos != null){ mInfos.add(index,t); } } if (mNotifyOnChange) notifyDataSetChanged(); } public void sort(Comparator<? super D> comparator){ synchronized (mLock){ if(isEmpty()){ return; } if(mInfos != null){ Collections.sort(mInfos, comparator); } } if (mNotifyOnChange) notifyDataSetChanged(); } public void hideLastPositionView(int position, View v) { v.setVisibility(position == getCount() - 1 ? View.GONE : View.VISIBLE); } public void lastPositionDividerFull(int position, View v, int margin) { LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, DensityUtil.dip2px(context, 0.6f)); if (position == getCount() - 1) { params.setMargins(0, 0, 0, 0); } else { params.setMargins(margin, 0, 0, 0); } v.setLayoutParams(params); } @SuppressWarnings("unchecked") protected static <T extends View> T findViewById(View view, int id) { return (T) view.findViewById(id); }}
我的微信二维码如下
微信订阅号二维码如下:
0 2
- BaseAdapter封装优化
- 116.黑名单设置的优化(封装BaseAdapter的方法)
- BaseAdapter优化
- BaseAdapter优化
- BaseAdapter优化
- BaseAdapter优化
- Android ListView封装(代码优化):抽取方法共性,封装 BaseAdapter 和 ViewHolder
- BaseAdapter的简单封装
- 封装BaseAdapter 实现类中复用
- BaseAdapter的简单封装
- BaseAdapter简单封装
- 对BaseAdapter进行封装
- BaseAdapter的封装
- BaseAdapter简单封装
- BaseAdapter简单封装
- baseadapter的二次封装
- BaseAdapter的封装
- listview baseadapter优化
- POJ1003 solve Hangover
- 多线程的几种控制方式
- opencv将图片变成固定比例,多余部分黑色填充
- 给你的组件添加Label
- Yii2框架源码追踪阅读(一)--从入口脚本index.php到类加载器的初始化
- BaseAdapter封装优化
- STL之Map完整(Linux内核)内部实现
- 那些年我用awk时踩过的坑——awk使用注意事项
- 排序
- LeetCode-141. Linked List Cycle
- [贪心 枚举] BZOJ 3214 [Zjoi2013]丽洁体
- 计算机原理学习(1)-- 冯诺依曼体系和CPU工作原理
- HDU2444 【二分图判定+最大匹配】
- jsp页面的分页实现