Home » Android » RecyclerView Item Click Listener with DataBinding

RecyclerView Item Click Listener with DataBinding

Posted by: admin November 1, 2017 Leave a comment

Questions:

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

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

my pojo class

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>

Adapter

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

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