I'm trying to set an onClickListener on one of my image on my recyclerview but the click doesn't work.
It looks like it doesn't seems to see there is a onClickListener, because when I set a "point" for the debugger on my listener it doesn't go to the point.
What I have done wrong ?
public class Item_List_Adapter extends RecyclerView.Adapter<Item_List_Adapter.MyViewHolder> {
// declare array
private String[] mDataset;
private Context mContext;
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView itemName;
public ImageView mAddBtn;
public MyViewHolder(View v){
super(v);
itemName = (TextView) v.findViewById(R.id.item_name);
mAddBtn = (ImageView) v.findViewById(R.id.action_add);
}
@Override
public void onClick(View v) {
System.out.println("TEST: ");
switch (v.getId()) {
case R.id.action_add:
System.out.println("TEST2: ");
break;
default:
break;
}
}
}
// constructor
public Item_List_Adapter(Context context, String[] myDataset) {
mDataset = myDataset;
mContext = context;
}
@Override
public int getItemCount() {
return mDataset.length;
}
@Override
public Item_List_Adapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);
MyViewHolder nv = new MyViewHolder(v);
return nv;
}
@Override
public void onBindViewHolder(Item_List_Adapter.MyViewHolder holder, final int position) {
holder.itemName.setText(mDataset[position]);
holder.mAddBtn.setOnClickListener(holder);
}
}
and my XML file the one inflate in the recycler view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<LinearLayout
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginEnd="15dp"
android:layout_marginStart="15dp">
<ImageView
android:id="@+id/action_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_check"
android:alpha="0.3"
/>
<TextView
android:id="@+id/item_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".2"
android:hint="Wine"
android:alpha="0.3"
android:gravity="start|center"
android:paddingStart="5dp"
android:paddingLeft="5dp"/>
<ImageView
android:id="@+id/action_remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_remove_green"
android:onClick="item_remove"
/>
<ImageView
android:id="@+id/action_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_add_blue"
/>
<ImageView
android:id="@+id/action_chart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_chart"
android:scaleType="centerInside"
android:onClick="chart_detail"
/>
</LinearLayout>
</LinearLayout>
Thanks in advance