具有DataBinding的RecyclerView项Click侦听器 [英] RecyclerView Item Click Listener with DataBinding
问题描述
我已经使用baseadapter实现了带有数据绑定的回收站视图,该适配器可处理任何布局项目的所有绑定.
I have implemented recycler view with data binding using a baseadapter which handles all the binding of any layout item.
我尝试使用方法引用和侦听器绑定实现每项单击侦听器,但是我无法做到这一点.
I have tried to implement per item click listener using method reference and Listener bindings, but I couldn't do that.
这是我的代码.您能给我一个示例,它是检测回收站视图中每个项目的简单方法,并且我想为每个项目添加单击侦听器以用于不同的目的.谢谢.
Here is my code. Can you give me a sample which is the simple way to detect every single item of the recycler view and I want to add click listener for every single item for different purposes. Thanks.
MyBaseAdapter
MyBaseAdapter
public abstract class MyBaseAdapter extends RecyclerView.Adapter<MyBaseAdapter.MyViewHold> {
public class MyViewHold extends RecyclerView.ViewHolder implements View.OnClickListener {
public ViewDataBinding binding;
Context context;
public MyViewHold(ViewDataBinding binding) {
super(binding.getRoot());
this.binding = binding;
context = binding.getRoot().getContext();
binding.getRoot().setOnClickListener(this);
}
// Here BR.pojo must be matched to layout variable name
//our layout variable was
/*
<variable
name="pojo"
type="com.durbinlabs.databinding.POJO"
/>
*/
public void bind(Object obj) {
binding.setVariable(BR.pojo, obj);
binding.setVariable(BR.food, obj);
binding.executePendingBindings();
}
@Override
public void onClick(View view) {
// for all view
}
}
@Override
public MyViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
ViewDataBinding binding = DataBindingUtil.inflate(layoutInflater, getLayoutIdForType(viewType)
, parent, false);
return new MyBaseAdapter.MyViewHold(binding);
}
@Override
public void onBindViewHolder(MyViewHold holder, int position) {
holder.bind(getDataAtPosition(position));
Log.d("click", "" + holder.getItemId());
}
public abstract Object getDataAtPosition(int position);
public abstract int getLayoutIdForType(int viewType);}
我的pojo班
public class POJO extends BaseObservable {
int img;
String name;
public POJO(int img) {
this.img = img;
}
public void setImg(int img) {
this.img = img;
}
public int getImg() {
return img;
}
public POJO(int img, String name) {
this.img = img;
this.name = name;
}
@Bindable
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
notifyPropertyChanged(BR.name);
}}
回收站视图行布局(每个项目)
recycler view row layout (per item)
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="pojo"
type="com.durbinlabs.databinding.POJO" />
</data>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:onClick="@{handlers::imgClick}"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_toRightOf="@id/icon"
android:ellipsize="marquee"
android:maxLines="1"
android:text="@{pojo.name}"
android:textColor="@android:color/black"
android:textSize="12sp" />
</RelativeLayout>
适配器
public class Adapter extends MyBaseAdapter {
private List<POJO> itemList;
Context context;
public Adapter(List<POJO> itemList) {
this.itemList = itemList;
}
public Adapter(List<POJO> itemList, Context context) {
this.itemList = itemList;
this.context = context;
}
@Override
public Object getDataAtPosition(int position) {
return itemList.get(position);
}
@Override
public int getLayoutIdForType(int viewType) {
return R.layout.rowlayout;
}
@Override
public int getItemCount() {
return itemList.size();
}}
推荐答案
在您的适配器类而不是基本适配器中尝试一下
Try this in your adapter class not base adapter
holder.binding.getRoot().findViewById(R.id.icon).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "clicked", Toast.LENGTH_SHORT).show();
}
}
);
这篇关于具有DataBinding的RecyclerView项Click侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!