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版