Android RecyclerView(五)封装Holder与Adapter(Android 5.0 新特性)

Android RecyclerView(五)封装Holder与Adapter(Android 5.0 新特性)

    


1 效果

列表样式  图示


2 BaseHolder的封装

public class BaseViewHolder  extends RecyclerView.ViewHolder {

    private SparseArray<View> views;

    public BaseViewHolder(View view) {
        super(view);
        this.views = new SparseArray<>();
    }

    public <T extends View> T getView(int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            view = itemView.findViewById(viewId);
            views.put(viewId, view);
        }
        return (T) view;
    }

    public View getRootView() {
        return itemView;
    }

}


3 BaseRecyclerViewAdapter 的封装
public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
    private Context context;
    private LayoutInflater inflater;
    private List<T> datas;
    private int layoutId;
    protected OnItemClickListner onItemClickListner;//单击事件
    protected OnItemLongClickListner onItemLongClickListner;//长按单击事件
    private boolean clickFlag = true;//单击事件和长单击事件的屏蔽标识

    /**
     * @param context  上下文对象
     * @param datas    数据
     * @param layoutId 布局ID
     */
    public BaseRecyclerViewAdapter(Context context, List<T> datas, int layoutId) {
        this.context = context;
        this.datas = datas;
        this.layoutId = layoutId;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建ViewHolder
        return new BaseViewHolder(inflater.inflate(layoutId, parent, false));
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        //数据绑定
        bindData(holder, datas.get(position), position);
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    protected abstract void bindData(BaseViewHolder holder, T data, int position);

    //设置Item点击事件
    public void setOnItemClickListner(OnItemClickListner onItemClickListner) {
        this.onItemClickListner = onItemClickListner;
    }

    //设置Item长按点击事件
    public void setOnItemLongClickListner(OnItemLongClickListner onItemLongClickListner) {
        this.onItemLongClickListner = onItemLongClickListner;
    }

    public interface OnItemClickListner {
        void onItemClickListner(View v, int position);
    }

    public interface OnItemLongClickListner {
        void onItemLongClickListner(View v, int position);
    }

}

4 使用
4.1 创建 CustomViewHolder
private class CustomViewHolder extends BaseRecyclerViewAdapter<String>{

    /**
     * @param context  上下文对象
     * @param datas    数据
     * @param layoutId 布局ID
     */
    public CustomViewHolder(Context context, List<String> datas, int layoutId) {
        super(context, datas, layoutId);
    }

    @Override
    protected void bindData(BaseViewHolder holder, String data, int position) {

       TextView textView =  holder.getRootView().findViewById(R.id.tv_item_text);

       textView.setText("测试数据 "+position);
    }
}
4.2 对RecyclerView 设置 CustomViewHolder
RecyclerView contentRecyclerView = (RecyclerView) findViewById(R.id.rv_main_content);
//初始化数据
List<String> list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
    list.add("智慧安卓 -- "+i);
}

//初始化Adapter
ListRecyclerAdapter recyclerAdapter = new ListRecyclerAdapter(this.getApplication(),list);
//初始化排列布局
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this.getApplicationContext());
//设置
contentRecyclerView.setLayoutManager(linearLayoutManager);
//设置水平排列
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//设置adapter
CustomViewHolder customViewHolder = new CustomViewHolder(this.getApplicationContext(), list, R.layout.item);

contentRecyclerView.setAdapter(customViewHolder);
早起的年轻人 CSDN认证博客专家 移动开发 项目管理 Java
只要用心去做,每一件事情还是有可能成功的,当然成功是没有界限的,只不过是达到自己心里的那个目标,公众号:我的大前端生涯,一个爱喝茶的程序员,通常会搞搞SpringBoot 、Herbinate、Mybatiys、Android、iOS、Flutter、Vue、小程序等.
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页