自定义的DataBindAdapter,日常使用

class DataBindAdapter<T>(
    var itemDatas: List<T>?,
    val defaultLayout: Int,
    val brId: Int

) : RecyclerView.Adapter<DataBindAdapter.BaseViewHolder>() {
    lateinit var mOnItemListener: OnItemListener<T>
    fun ismOnItemListener() = ::mOnItemListener.isInitialized

    fun getItemLayout(itemData: T): Int {
        return defaultLayout
    }

    fun onItemDatasChanged(newItemDatas: List<T>?) {
        itemDatas = newItemDatas
        notifyDataSetChanged()
    }
    interface OnItemListener<T> {
        fun addListener(root: View?, itemData: T, position: Int)
    }

    fun setOnItemListener(onitemListener: OnItemListener<T>) {
        this.mOnItemListener = onitemListener
    }

    //start 从这开始更新
    //itemcount 更新几个
    protected fun onItemRangeChanged(
        newItemDatas: List<T>?,
        positionStart: Int,
        itemCount: Int
    ) {
        itemDatas = newItemDatas
        notifyItemRangeChanged(positionStart, itemCount)
    }

    protected fun onItemRangeInserted(
        newItemDatas: List<T>?,
        positionStart: Int,
        itemCount: Int
    ) {
        itemDatas = newItemDatas
        notifyItemRangeInserted(positionStart, itemCount)
    }

    protected fun onItemRangeRemoved(
        newItemDatas: List<T>?,
        positionStart: Int,
        itemCount: Int
    ) {
        itemDatas = newItemDatas
        notifyItemRangeRemoved(positionStart, itemCount)
    }

    override fun getItemViewType(position: Int): Int {
        return getItemLayout(itemDatas!![position])
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BaseViewHolder {
        val binding = DataBindingUtil.inflate<ViewDataBinding>(
            LayoutInflater.from(parent.context),
            viewType,
            parent,
            false
        )
        return BaseViewHolder(binding)
    }

    override fun onBindViewHolder(
        holder: BaseViewHolder,
        position: Int
    ) {
        holder.binding.setVariable(brId, itemDatas!![position])
        holder.binding.executePendingBindings()

        if(ismOnItemListener()){
            var position = holder.layoutPosition
            holder.itemView.setOnClickListener {
                mOnItemListener!!.addListener(holder.binding.root, itemDatas!![position], position)
            }
        }

    }

    override fun getItemCount(): Int {
        return if (itemDatas == null) 0 else itemDatas!!.size
    }

    class BaseViewHolder(var binding: ViewDataBinding) :
        RecyclerView.ViewHolder(binding.root)

}
//使用
        var adapter = DataBindAdapter(
            mutableListOf(search_bean.TestBean(), search_bean.TestBean(), search_bean.TestBean()),
            R.layout.item_send_service,
            BR.DataBean
        )
//记得在布局文件定义好Bean
        adapter!!.setOnItemListener(object :
            DataBindAdapter.OnItemListener<search_bean.TestBean> {
            override fun addListener(
                root: View?,
                itemData: search_bean.TestBean,
                position: Int
            ) {

            }
        })
        binding.rv.adapter = adapter

Kotlin版

public class DataBindAdapter <T> extends RecyclerView.Adapter<DataBindAdapter.BaseViewHolder> {

    private List<T> itemDatas;
    private int defaultLayout;
    private int brId;

    public DataBindAdapter(List<T> itemDatas, int defaultLayout, int brId){
        this.itemDatas = itemDatas;
        this.defaultLayout = defaultLayout;
        this.brId = brId;
    }

    public int getItemLayout(T itemData){
        return defaultLayout;
    }

    public void addListener(View root, T itemData, int position){}

    public void onItemDatasChanged(List<T> newItemDatas){
        this.itemDatas = newItemDatas;
        notifyDataSetChanged();
    }

    //start 从这开始更新
    //itemcount 更新几个
    protected void onItemRangeChanged(List<T> newItemDatas, int positionStart, int itemCount)
    {
        this.itemDatas = newItemDatas;
        notifyItemRangeChanged(positionStart,itemCount);
    }

    protected void onItemRangeInserted(List<T> newItemDatas, int positionStart, int itemCount)
    {
        this.itemDatas = newItemDatas;
        notifyItemRangeInserted(positionStart,itemCount);
    }

    protected void onItemRangeRemoved(List<T> newItemDatas, int positionStart, int itemCount)
    {
        this.itemDatas = newItemDatas;
        notifyItemRangeRemoved(positionStart,itemCount);
    }

    @Override
    public int getItemViewType(int position) {
        return getItemLayout(itemDatas.get(position));
    }

    @Override
    public DataBindAdapter.BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), viewType, parent, false);
        return new BaseViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(DataBindAdapter.BaseViewHolder holder, int position) {
        holder.binding.setVariable(brId, itemDatas.get(position));
        holder.binding.executePendingBindings();
        addListener(holder.binding.getRoot(), itemDatas.get(position), position);
    }

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

    class BaseViewHolder extends RecyclerView.ViewHolder {
        ViewDataBinding binding;

        BaseViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
//使用
DataBindAdapter dataBindAdapter = new DataBindAdapter<getstorecategorybean.databean.ChildrenBean>
                        (beanList,R.layout.item_search_history, BR.DataBean){
                    @Override
                    public void addListener(View root, getstorecategorybean.databean.ChildrenBean itemData, int position) {


                    }
                };
                rv.setAdapter(dataBindAdapter);

JAVA版

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值