如何优雅的写代码——使用RecyleView的基类Adapter

使用基类是代码更清晰也更简单,就是容易看也容易写。平常写个Adapter可能又长又乱,现在可以这样写了:

public class AppListAdapter extends RecycleVariableAdapter {

    public AppListAdapter(Context context, List 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 extends RecyclerView.Adapter implements Variable {

    /** adapter 数据集 */
    public List data;
    /** Context */
    protected Context context;
    /** 用于解析布局 */
    protected LayoutInflater inflater;

    public RecycleVariableAdapter(Context context, List 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 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 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;
    }
}


这个基类还加了一些数据的处理方法,方便使用。


你可能感兴趣的:(Android开发架构系列)