如何优雅的写代码——使用RecyleView的基类Adapter
来源:互联网 发布:网络组建总结 编辑:程序博客网 时间:2024/05/29 16:38
使用基类是代码更清晰也更简单,就是容易看也容易写。平常写个Adapter可能又长又乱,现在可以这样写了:
public class AppListAdapter extends RecycleVariableAdapter<AppViewHolder, AppInfo> { public AppListAdapter(Context context, List<AppInfo> data) { super(context, data); } @Override public AppViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = inflater.inflate(R.layout.item_app_list, parent, false); return new AppViewHolder(view); } @Override public void onBindViewHolder(AppViewHolder holder, int position) { final AppInfo appInfo = data.get(position); holder.appName.setText(appInfo.getName()); holder.appSize.setText(appInfo.getSize()); WpPicasso.with(context) .load(appInfo.getIcon()) .placeholder(R.drawable.ic_appstore_default_icon) .into(holder.icon); }}是不是很简单,不过这里还需要自定义一个ViewHolder,像这样:
class AppViewHolder extends RecyclerHolder { ImageView icon; TextView appName; TextView appSize; public AppViewHolder(View view) { super(view); icon = ViewFinder.find(view, R.id.icon); appName = ViewFinder.find(view, R.id.app_name); appSize = ViewFinder.find(view, R.id.app_size); }}就三个步骤,建一个ViewHolder;为ViewHoder绑定view;根据data设置ViewHolder的View。
这个基类写也很简单,还可以自己添加项目所需的方法进去。下面是我使用的基类,还是很简单的。
public abstract class RecycleVariableAdapter<Holder extends RecyclerHolder, T> extends RecyclerView.Adapter<Holder> implements Variable<T> { /** adapter 数据集 */ public List<T> data; /** Context */ protected Context context; /** 用于解析布局 */ protected LayoutInflater inflater; public RecycleVariableAdapter(Context context, List<T> data) { this.context = context; this.data = data; inflater = LayoutInflater.from(context); } public Context getContext(){ return this.context; } @Override public int getItemCount() { return data != null ? data.size() : 0; } /** * 是否是个空的 * @return */ public boolean isEmpty(){ return getItemCount() == 0; } //兼容处理. public T getItem(int position){ return data.get(position); } /** * 判断非空 * @param adapter * @return */ public static boolean checkEmpty(RecycleVariableAdapter adapter){ return adapter == null || adapter.isEmpty(); } @Override public void refresh(List<T> data) { this.data.clear(); this.data = data; notifyDataSetChanged(); } @Override public void append(T data) { this.data.add(data); notifyItemInserted(this.data.size() - 1); } @Override public void append(int position, T data) { this.data.add(position,data); notifyItemInserted(position); } @Override public void append(List<T> data) { int oldSize = this.data.size(); this.data.addAll(data); notifyItemRangeInserted(oldSize, data.size()); } @Override public T remove(T item) { this.data.remove(item); notifyItemRemoved(this.data.size() + 1); return item; } @Override public T remove(int position) { T item = this.data.get(position); this.data.remove(position); notifyItemRemoved(position); return item; } public void removeAll() { this.data.clear(); notifyDataSetChanged(); }}
public class RecyclerHolder extends RecyclerView.ViewHolder { public View rootView; public RecyclerHolder(View itemView) { super(itemView); rootView = itemView; }}
这个基类还加了一些数据的处理方法,方便使用。
1 0
- 如何优雅的写代码——使用RecyleView的基类Adapter
- 如何写优雅的代码(序)——自语
- 如何写优雅的代码—#define?const?还是enum?
- 如何写优雅的代码(1)——灵活使用goto和__try
- 如何写优雅的代码(1)——灵活使用goto和__try:评论反馈
- 如何写优雅的代码(1)——灵活使用goto和__try
- 如何写优雅的代码(1)——灵活使用goto和__try
- 如何写优雅的代码(1)——灵活使用goto和__try:评论反馈
- 如何写优雅的代码(1)——灵活使用goto和__try
- 如何优雅的写好Pythonic代码?
- 如何使用Markdown优雅的写博客
- 如何写优雅的代码(2)——#define?const?还是enum?
- 如何写优雅的代码(3)——合理选择函数形参
- 如何写优雅的代码(2)——#define?const?还是enum?
- 如何写优雅的代码(3)——合理选择函数形参
- 如何写优雅的代码(4)——简单有效地玩转线程
- 如何写优雅的代码(5)——远离临界区噩梦
- 如何写优雅的代码(5)——远离临界区噩梦
- HTTP Client
- 字段和属性
- 虚函数及继承
- 使用phpnow启动mysql时出现Install/Remove of the Service Denied!错误
- 自己动手设计数据库——互动出版网
- 如何优雅的写代码——使用RecyleView的基类Adapter
- word插入图片显示不全
- 进程、线程、任务
- [6]禁止所有Activity横屏
- 并发编程实践 -- 笔记
- CPicture类的使用方法
- IntelliJ IDEA 使用心得与常用快捷键
- 程序调试VC++ 6.0
- HTML5开发Webapp总结