BaseAdapter抽取

BaseAdapter

public abstract class BaseAdapter<T> extends RecyclerView.Adapter {

    Context context;
    List<T> datalist = new ArrayList<>();

    public BaseAdapter(Context context) {
        this.context = context;
    }

    public void resetDataList(List<T> datalist) {
        this.datalist.clear();
        this.datalist.addAll(datalist);
        notifyDataSetChanged();
    }

    public void addDataList(List<T> datalist) {
        this.datalist.addAll(datalist);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(context).inflate(getLayout(),parent,false);

        return new BaseViewHolder(root);
    }

    protected abstract int getLayout();

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final BaseViewHolder baseViewHolder = (BaseViewHolder) holder;

        bindData(baseViewHolder,datalist.get(position));

        baseViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (iOnClickItem!=null){
                    iOnClickItem.clickItem(baseViewHolder,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return datalist.size();
    }

    protected abstract void bindData(BaseViewHolder baseViewHolder, T t);

    static class BaseViewHolder extends RecyclerView.ViewHolder {

        SparseArray<View>  views;
        public BaseViewHolder(@NonNull View itemView) {
            super(itemView);
            if (views==null){

                views = new SparseArray<>();

            }
        }

        public View getViewById(int id){

            View view = views.get(id);

            if (view==null){
                view = itemView.findViewById(id);
                views.append(id,view);
            }
            return view;
        }
    }


    IOnClickItem iOnClickItem;

    public void setiOnClickItem(IOnClickItem iOnClickItem) {
        this.iOnClickItem = iOnClickItem;
    }

    interface IOnClickItem{
        void clickItem(BaseViewHolder viewHolder,int pos);
    }
}

实现类

public class TopicAdapter extends BaseAdapter<TopicBean.DataBean> {

    public TopicAdapter(Context context) {
        super(context);
    }

    @Override
    protected int getLayout() {
        return R.layout.topic_list_item;
    }

    @Override
    protected void bindData(BaseViewHolder baseViewHolder, TopicBean.DataBean dataBean) {
//        img = itemView.findViewById(R.id.image);
        ImageView img = (ImageView) baseViewHolder.getViewById(R.id.image);
        TextView title = (TextView) baseViewHolder.getViewById(R.id.title);
        TextView stitle = (TextView) baseViewHolder.getViewById(R.id.sub_title);
        TextView ptitle = (TextView) baseViewHolder.getViewById(R.id.price_info);

        Glide.with(context).load(dataBean.getScene_pic_url()).into(img);
        title.setText(dataBean.getTitle());
        stitle.setText(dataBean.getSubtitle());
        ptitle.setText(StringUtils.filterStr(dataBean.getPrice_info())+"元起");

    }
 
}

调用

     topicAdapter = new TopicAdapter(context);
        recyclerView.setAdapter(topicAdapter);
   topicAdapter.addDataList(result.getData().getData());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值