Android BaseBindingAdapter

package com.iflytek.jzapp.base;

import android.content.Context;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * RecyclerView的适配器,封装有上拉加载,下拉刷新
 *
 * @author: LiuChunqiu
 * @date:  2021/02/26
 */
public abstract class BaseBindingAdapter extends RecyclerView.Adapter {
    public List mDataList;
    public Context mContext;

    public BaseBindingAdapter(Context context) {
        mContext = context;
        mDataList = new ArrayList<>();
    }

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

    public T getItem(int position){
        return mDataList.get(position);
    }

    public List getItems(){
        return mDataList;
    }

    @NotNull
    @Override
    public VH onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        return onCreateVH(parent, viewType);
    }

    @Override
    public void onBindViewHolder(@NotNull VH holder, int position) {
        onBindVH(holder, position);
    }

    /**
     * 刷新数据
     *
     * @param list
     */
    public void onRefreshData(List list) {
        if (mDataList != null) {
            mDataList.clear();
            mDataList.addAll(list);
            notifyDataSetChanged();
        }
    }

    /**
     * 加载更多
     *
     * @param list
     */
    public void onLoadMoreData(List list) {
        if (mDataList != null && list != null) {
            mDataList.addAll(list);
            notifyDataSetChanged();
        }
    }

    /**
     * 创建ViewHolder的抽象方法
     *
     * @param parent
     * @param viewType
     * @return
     */
    public abstract VH onCreateVH(ViewGroup parent, int viewType);

    /**
     * 绑定ViewHolder的抽象方法
     *
     * @param holder
     * @param position
     */
    public abstract void onBindVH(VH holder, int position);

}
package com.iflytek.jzapp.cloud.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.iflytek.jzapp.BR;
import com.iflytek.jzapp.base.BaseBindViewHolder;
import com.iflytek.jzapp.base.BaseBindingAdapter;
import com.iflytek.jzapp.cloud.listener.OnItemClickListener;

/**
 * 通用Adapter
 *
 * @author: LiuChunqiu
 * @date: 2021/03/23
 */
public abstract class BaseCloudAdapter extends BaseBindingAdapter {
    public final OnItemClickListener mOnItemClickListener;
    public String mSearchKey = null;

    public BaseCloudAdapter(Context context, OnItemClickListener onItemClickListener) {
        super(context);
        mOnItemClickListener = onItemClickListener;
    }

    @Override
    public BaseBindViewHolder onCreateVH(ViewGroup parent, int viewType) {
        ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), getLayoutId(), parent, false);
        return new BaseBindViewHolder(binding);
    }

    @Override
    public void onBindVH(BaseBindViewHolder holder, int position) {
        ViewDataBinding binding = holder.getBinding();
        binding.setVariable(BR.itemData, mDataList.get(position));
        binding.setVariable(BR.itemClick, mOnItemClickListener);
        binding.setVariable(BR.itemPosition, position);
        binding.setVariable(BR.count, mDataList.size());
        binding.executePendingBindings();
    }

    /**
     * Item布局文件
     *
     * @return 布局id
     */
    protected abstract int getLayoutId();

    public void setSearchKey(String keyword) {
        this.mSearchKey = keyword;
    }
}
package com.iflytek.jzapp.base;

import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Adapter的ViewHolder
 *
 * @author: LiuChunqiu
 * @date:  2021/02/26
 */
public class BaseBindViewHolder extends RecyclerView.ViewHolder {
    ViewDataBinding mDataBinding;

    /**
     * 默认构造方法
     * @param binding ViewDataBinding
     */
    public BaseBindViewHolder(ViewDataBinding binding) {
        super(binding.getRoot());
        mDataBinding = binding;
    }

    /**
     * 获取ViewDataBinding
     * @return ViewDataBinding
     */
    public ViewDataBinding getBinding() {
        return mDataBinding;
    }
}

你可能感兴趣的:(android)