RecyclerView项目单击带有DataBinding的Listener

时间:2017-10-22 13:08:44

标签: android data-binding android-recyclerview android-databinding

我使用baseadapter实现了带有数据绑定的回收站视图,它可以处理任何布局项的所有绑定。

我尝试使用方法引用和侦听器绑定来实现每个项目单击侦听器,但我无法做到这一点。

这是我的代码。你能给我一个样本,这是检测回收者视图的每个项目的简单方法,我想为每个项目添加点击监听器以用于不同的目的。感谢。

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);
}}

回收者视图行布局(每个项目)

<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();
}}

1 个答案:

答案 0 :(得分:4)

在适配器类中尝试此操作,而不是基本适配器

  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();
                }
            }
    );