如何在 BaseRecyclerAdapter 中设置 itemclick 侦听器
How to set itemclick listener in a BaseRecyclerAdapter
我有一个像这样的基本回收器适配器:
public abstract class BaseRecyclerViewAdapter extends RecyclerView.Adapter<BaseViewHolder> {
public @NotNull BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
ViewDataBinding binding = DataBindingUtil.inflate(layoutInflater, viewType, parent, false);
return new BaseViewHolder(binding);
}
public void onBindViewHolder(BaseViewHolder holder, int position) {
Object obj = getObjForPosition(position);
OnItemClickListener clickListener = onItemClickListener();
holder.bind(obj, clickListener, position);
}
@Override
public int getItemViewType(int position) {
return getLayoutIdForPosition(position);
}
protected abstract Object getObjForPosition(int position);
protected abstract int getLayoutIdForPosition(int position);
protected abstract OnItemClickListener onItemClickListener();
}
OnItemClickListener
:
public interface OnItemClickListener {
void onItemClick(Object object, View view, int position);
}
基础视图持有者是:
public class BaseViewHolder extends RecyclerView.ViewHolder {
private final ViewDataBinding binding;
public BaseViewHolder(ViewDataBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
public void bind(Object obj, OnItemClickListener clickListener, int position) {
itemView.setOnClickListener(v -> clickListener.onItemClick(obj, itemView, position));
binding.setVariable(BR.obj, obj);
binding.executePendingBindings();
}
}
假设 ViewHolder 有 2 个视图,一个 TextView 和一个 ImageView。如何对 ViewHolder
的项目视图内的所有项目实施 ClickListener
我认为我们不能从 base adapter.but 为 itemView 中的特定项目设置 clickListener,而是可以将布局的侦听器设置为变量。
为此,在布局的数据标记中定义 clickListener 变量。
<data>
<variable
name="clickListener"
type="com.package.clickListener" />
</data>
然后为来自基本适配器的布局设置 clickListener
public void bind(Object obj, OnItemClickListener clickListener, int position) {
itemView.setOnClickListener(v -> clickListener.onItemClick(obj, itemView, position));
binding.setVariable(BR.clickListener,clickListener)//set listener here
binding.setVariable(BR.obj, obj);
binding.executePendingBindings();
}
然后您可以在特定项目被点击时调用侦听器方法。
<Button
android:id="@+id/btn"
android:onClick="@{()-> callListener.onItemClick()}"
/>
我有一个像这样的基本回收器适配器:
public abstract class BaseRecyclerViewAdapter extends RecyclerView.Adapter<BaseViewHolder> {
public @NotNull BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
ViewDataBinding binding = DataBindingUtil.inflate(layoutInflater, viewType, parent, false);
return new BaseViewHolder(binding);
}
public void onBindViewHolder(BaseViewHolder holder, int position) {
Object obj = getObjForPosition(position);
OnItemClickListener clickListener = onItemClickListener();
holder.bind(obj, clickListener, position);
}
@Override
public int getItemViewType(int position) {
return getLayoutIdForPosition(position);
}
protected abstract Object getObjForPosition(int position);
protected abstract int getLayoutIdForPosition(int position);
protected abstract OnItemClickListener onItemClickListener();
}
OnItemClickListener
:
public interface OnItemClickListener {
void onItemClick(Object object, View view, int position);
}
基础视图持有者是:
public class BaseViewHolder extends RecyclerView.ViewHolder {
private final ViewDataBinding binding;
public BaseViewHolder(ViewDataBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
public void bind(Object obj, OnItemClickListener clickListener, int position) {
itemView.setOnClickListener(v -> clickListener.onItemClick(obj, itemView, position));
binding.setVariable(BR.obj, obj);
binding.executePendingBindings();
}
}
假设 ViewHolder 有 2 个视图,一个 TextView 和一个 ImageView。如何对 ViewHolder
的项目视图内的所有项目实施 ClickListener我认为我们不能从 base adapter.but 为 itemView 中的特定项目设置 clickListener,而是可以将布局的侦听器设置为变量。
为此,在布局的数据标记中定义 clickListener 变量。
<data>
<variable
name="clickListener"
type="com.package.clickListener" />
</data>
然后为来自基本适配器的布局设置 clickListener
public void bind(Object obj, OnItemClickListener clickListener, int position) {
itemView.setOnClickListener(v -> clickListener.onItemClick(obj, itemView, position));
binding.setVariable(BR.clickListener,clickListener)//set listener here
binding.setVariable(BR.obj, obj);
binding.executePendingBindings();
}
然后您可以在特定项目被点击时调用侦听器方法。
<Button
android:id="@+id/btn"
android:onClick="@{()-> callListener.onItemClick()}"
/>