0

I have recyclerview with a listener. I use this code :

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {


   //just setting the textview etc



    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Model_NewProg model_newProg = ListOfExo.get(position);
            openDialog(model_newProg, position);
        }
    });
}

and my xml for the adapter is :

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="5dp"
    card_view:cardBackgroundColor="#FFFFFF"
    card_view:cardCornerRadius="5dp">



    <RelativeLayout
        android:layout_width="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:layout_height="wrap_content">

        <View
            android:id="@+id/view_space"
            android:layout_width="match_parent"
            android:background="@color/common_google_signin_btn_text_light_pressed"
            android:layout_height="5dp">


        </View>
    <LinearLayout
        android:id="@+id/lay1"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_below="@id/view_space"
        android:layout_height="wrap_content">



        <TextView
            android:id="@+id/lbl_exo_name"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="40dp"
            android:gravity="center"
            android:textColorHint="#aeafaf"
            android:textSize="15dp" />

        <TextView
            android:id="@+id/lbl_temps_pause"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="40dp"
            android:inputType="number"
            android:gravity="center"
            android:textColorHint="#aeafaf"
            android:textSize="15dp" />


    </LinearLayout>



    <LinearLayout
        android:id="@+id/lay2"
        android:layout_width="match_parent"
        android:layout_below="@id/lay1"
        android:orientation="horizontal"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/lbl_nbr_séries"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="40dp"
            android:inputType="number"
            android:gravity="center"
            android:textColorHint="#aeafaf"
            android:textSize="15dp" />

        <TextView
            android:id="@+id/lbl_nbr_rep"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:inputType="number"
            android:gravity="center"
            android:textColorHint="#aeafaf"
            android:textSize="15dp" />

    </LinearLayout>


    </RelativeLayout>


</android.support.v7.widget.CardView>

for some reason, if I click on the cell the listener on the holder doesn't trigger. Well sometimes it triggers if I aim the corner of the textview. But it seems that the text view is "blocking" the listener... I have to try to add android:clickable=false; android:clickable=true; ,... not working

Does anyone has an idea? I use this code everytime and it always work

ImNeos
  • 527
  • 6
  • 17

3 Answers3

0

The holder.itemView must be changed to : holder.theIdOfTheElementYouWantToClick

coroutineDispatcher
  • 7,718
  • 6
  • 30
  • 58
0

you need to implement View.onClickListener in ViewHolder class then register it in the constructure see this answer for more details

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{


    public ViewHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
}
 @Override
        public void onClick(View v) {
//do what you want
}

    }
Mohammad Sommakia
  • 1,773
  • 3
  • 15
  • 48
  • it doesn't work. if I put listener to all the text view, it works but that's not what I'am looking for. I want that when I click anywhere on the cell it triggers the listener. Normally the holder.ItemView.setOnClickListener should work but I don't understand what is the problem here – ImNeos Jun 19 '19 at 13:52
  • so you have to register the itemview it self to do what you want and this is the correct way because putting a lisener in onBindViewHolder it is bad idea – Mohammad Sommakia Jun 19 '19 at 13:55
  • see this https://stackoverflow.com/questions/33845846/why-is-adding-an-onclicklistener-inside-onbindviewholder-of-a-recyclerview-adapt – Mohammad Sommakia Jun 19 '19 at 13:56
  • yes I understand, i'm trying to do your way but in the onCreateViewHolder the line ViewHolder viewHolder = new ViewHolder(v); isn't working. It forces me to write : " RecyclerView.ViewHolder viewHolder = new RecyclerView.ViewHolder(v); " and it says that ViewHolder is abstract it can't be instantiated Maybe I miss a library ? – ImNeos Jun 19 '19 at 14:05
  • @MohammadSommakia you coppied my answer..its not ImageView its CardView.. ;) – Prachi Singh Jun 19 '19 at 14:16
  • @MohammadSommakia how it is different from my code for compiler – Prachi Singh Jun 19 '19 at 14:21
  • because you are setting the listener in the constructure but i override it from the interface – Mohammad Sommakia Jun 19 '19 at 14:23
0

Assign id to your cardView and fetch it in ViewHolder class and perform onClick on this id within ViewHolder class. For example.. if your cardView id is cardView

class ViewHolder extends RecyclerView.ViewHolder {

    public CardView rowCardView;

    public ViewHolder(View itemView) {
        super(itemView);
        rowCardView = (CardView) itemView.findViewById(R.id.cardView);
        rowCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //add your code..
            }
        });

    }}
Prachi Singh
  • 564
  • 3
  • 8
  • It is not working It's weird, only works if I hit the left corner of the cell – ImNeos Jun 19 '19 at 14:09
  • I am pretty sure that it is a problem of parent or something like this I even tried to put on the onBindViewHolder method a listener to the CardView and still it doesn't trigger, just like the textview took the click instead of the CardView – ImNeos Jun 19 '19 at 14:11
  • well it is happening – ImNeos Jun 19 '19 at 14:11
  • @ImNeos try by giving RelativeLayout width = match_parent – Prachi Singh Jun 19 '19 at 14:14