Android中的Adapter封装
2016-05-08 16:37
316 查看
转载请注明出处:/article/10395501.html
在Android开发中经常会用到Adapter,经常要实现一大堆方法。为了简化开发,不多做一些简单繁琐的工作,我对Adapter进行了封装。
代码如下:(其中除了一大堆常用的view的api之外,关键代码在前面几个方法,构造函数ViewHolder(View)通过传入一个View来持有这个View,findViewById,通过id定位子控件)
使用方法:
写一个类直接继承EasyAdapter声明的泛型为你要处理的数据的类型,然后实现convert方法处理数据的绑定
源代码:因为基本上的代码都在上面了,看懂的都能直接用了,这里就不提供项目代码了
在Android开发中经常会用到Adapter,经常要实现一大堆方法。为了简化开发,不多做一些简单繁琐的工作,我对Adapter进行了封装。
/** * 要先在构造函数中调用setLayoutId()设置布局文件 * * @author zhanjixun * * @param <T> */ public abstract class EasyAdapter<T> extends BaseAdapter { protected List<T> mList = new LinkedList<T>(); protected Context mContext; protected int mLayoutId; public EasyAdapter(Context context, List<T> list) { mContext = context; mList = list; } /** * 获取适配器的数据 * * @return */ public List<T> getList() { return mList; } /** * 单个添加数据 * * @param item */ public void add(T item) { mList.add(item); notifyDataSetChanged(); } /** * 追加数据 * * @param list */ public void append(List<T> list) { mList.addAll(list); notifyDataSetChanged(); } /** * 替换数据,不关联参数的List * * @param list */ public void replace(List<T> list) { mList.clear(); mList.addAll(list); notifyDataSetChanged(); } /** * 清空数据 */ public void clear() { mList.clear(); notifyDataSetChanged(); } @Override public int getCount() { return mList.size(); } @Override public T getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } public Context getContext() { return mContext; } public int getLayoutId() { return mLayoutId; } protected void setLayoutId(int layoutId) { mLayoutId = layoutId; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { View view = convertView = View.inflate(mContext, mLayoutId, null); holder = new ViewHolder(view); view.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } // 填充信息 convert(position, holder, getItem(position)); return holder.getConvertView(); } protected abstract void convert(int position, ViewHolder holder, T item); }其中用到的ViewHolder是用来保存重用的view的,可以在这里设置特定的item的数据
代码如下:(其中除了一大堆常用的view的api之外,关键代码在前面几个方法,构造函数ViewHolder(View)通过传入一个View来持有这个View,findViewById,通过id定位子控件)
/** * view持有 * * @author zhanjixun * */ public class ViewHolder { private final SparseArray<View> mViews = new SparseArray<View>(); private View mConvertView; public ViewHolder(View v) { mConvertView = v; } /** * 通过viewId获取控件 * * @param viewId * @return */ @SuppressWarnings("unchecked") public <T extends View> T findViewById(int viewId) { View view = mViews.get(viewId); if (view == null) { view = mConvertView.findViewById(viewId); mViews.put(viewId, view); } return (T) view; } /** * 得到持有的View * * @return */ public View getConvertView() { return mConvertView; } public ViewHolder setText(int viewId, String text) { TextView tv = findViewById(viewId); tv.setText(text); return this; } public ViewHolder setImageResource(int viewId, int resId) { ImageView view = findViewById(viewId); view.setImageResource(resId); return this; } public ViewHolder setImageBitmap(int viewId, Bitmap bitmap) { ImageView view = findViewById(viewId); view.setImageBitmap(bitmap); return this; } public ViewHolder setImageDrawable(int viewId, Drawable drawable) { ImageView view = findViewById(viewId); view.setImageDrawable(drawable); return this; } public ViewHolder setBackgroundColor(int viewId, int color) { View view = findViewById(viewId); view.setBackgroundColor(color); return this; } public ViewHolder setBackgroundResource(int viewId, int backgroundRes) { View view = findViewById(viewId); view.setBackgroundResource(backgroundRes); return this; } public ViewHolder setTextColor(int viewId, int textColor) { TextView view = findViewById(viewId); view.setTextColor(textColor); return this; } public ViewHolder setTextColorResource(int viewId, int textColorRes) { TextView view = findViewById(viewId); Resources resources = mConvertView.getContext().getResources(); view.setTextColor(resources.getColor(textColorRes)); return this; } public ViewHolder setVisibility(int viewId, int visible) { View view = findViewById(viewId); view.setVisibility(visible); return this; } public ViewHolder setProgress(int viewId, int progress) { ProgressBar view = findViewById(viewId); view.setProgress(progress); return this; } public ViewHolder setProgress(int viewId, int progress, int max) { ProgressBar view = findViewById(viewId); view.setMax(max); view.setProgress(progress); return this; } public ViewHolder setProgressMax(int viewId, int max) { ProgressBar view = findViewById(viewId); view.setMax(max); return this; } public ViewHolder setRating(int viewId, float rating) { RatingBar view = findViewById(viewId); view.setRating(rating); return this; } public ViewHolder setRating(int viewId, float rating, int max) { RatingBar view = findViewById(viewId); view.setMax(max); view.setRating(rating); return this; } public ViewHolder setTag(int viewId, Object tag) { View view = findViewById(viewId); view.setTag(tag); return this; } public ViewHolder setTag(int viewId, int key, Object tag) { View view = findViewById(viewId); view.setTag(key, tag); return this; } public ViewHolder setChecked(int viewId, boolean checked) { Checkable view = (Checkable) findViewById(viewId); view.setChecked(checked); return this; } public ViewHolder setOnClickListener(int viewId, View.OnClickListener listener) { View view = findViewById(viewId); view.setOnClickListener(listener); return this; } public ViewHolder setOnTouchListener(int viewId, View.OnTouchListener listener) { View view = findViewById(viewId); view.setOnTouchListener(listener); return this; } public ViewHolder setOnLongClickListener(int viewId, View.OnLongClickListener listener) { View view = findViewById(viewId); view.setOnLongClickListener(listener); return this; } }
使用方法:
写一个类直接继承EasyAdapter声明的泛型为你要处理的数据的类型,然后实现convert方法处理数据的绑定
public class ListViewAdapter extends EasyAdapter<String> { public ListViewAdapter(Context context, List<String> list) { super(context, list); setLayoutId(R.layout.item_listview); } @Override public void convert(int position, ViewHolder holder, String item) { super.convert(position, holder, item); holder.setText(R.id.mainTextView, item); } }
源代码:因为基本上的代码都在上面了,看懂的都能直接用了,这里就不提供项目代码了
相关文章推荐
- Ubuntu下载Android源码
- android 布局属性
- Android-BroadcastReceiver广播的用法
- studyjams 3B better android
- Gradle DSL method not found android
- Android Handler Message 在使用多个Handler时,接收不到消息的处理办法
- Android通用流行框架大全
- android textview 自动换行 整齐排版
- android学习的两三事- studyjams论坛学习有感
- android java 与C 通过 JNI双向通信
- android-ndk-r11b/RELEASE.TXT 文件获取 NDK 版本失败。
- 66^3 工作室作品展示:绿意满屋
- Android开发屏幕适配方案
- android 使用外部字体
- Android 手动显示和隐藏软键盘
- Android SlidingMenu 环境配置
- Volley源码之调用流程及各类作用
- Android读书笔记——Android studio的安装配置
- Android setLayoutParams 常见问题汇总
- android adapter公共写法