Skip to content Skip to sidebar Skip to footer

Issue With Cardview And Onclicklistener In Recyclerview

I am using CardView layout as Row for RecyclerView. However I am facing issue in attaching OnClickListener to the layout. I am using following layout

Solution 1:

Actually implementing onClick is very straightforward and I am not sure of the implementation of a custom click handler (IClickHandler).

First of all View holder should be just:

staticclassSampleViewHolderextendsRecyclerView.ViewHolder {
  TextView name;

  publicSampleViewHolder(View itemView) {
    super(itemView);
    name = (TextView) itemView.findViewById(R.id.name);
  }
}

And then in SampleAdapter

@Overridepublic SampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  Viewview= inflater.inflate(R.layout.layout1, parent, false);
  SampleViewHolderholder=newSampleViewHolder(view);
  return holder;
}

@OverridepublicvoidonBindViewHolder(SampleViewHolder holder, int position) {
    finalItemitem= arrayList.get(position);

    holder.name.setOnClickListener(newView.OnClickListener(){

       @OverridepublicvoidonClick(View view) {
         //notice I implemented onClickListener here// so I can associate this click with final Item item           
       }

    });

}

Solution 2:

Perfect Generic Solution

**Step 1:**Go to xml where the cardview is placed.Assign it a id

<android.support.v7.widget.CardView
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/activity_vertical_margin"
    app:cardCornerRadius="@dimen/activity_vertical_margin"
    app:cardElevation="@dimen/activity_vertical_margin"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"/>

**Step 2:**Inside View holder class get hold of the id of card view

public class View_Holder_For_Grocery extends RecyclerView.ViewHolder { CardView cv;

View_Holder_For_Grocery(View itemView) {
    super(itemView);
    cv = (CardView) itemView.findViewById(R.id.cardView);
}

}

**Step 3:**Inside Recycler_View_Adapter class the onBindViewHolder method the onClick is as below

  @Override
    publicvoidonBindViewHolder(View_Holder_For_Grocery holder, finalint position){

        holder.cv.setOnClickListener(new View.OnClickListener() {
            @Override
            publicvoidonClick(View v) {
                switch (position){
                    case0:
                        Intent intent0=newIntent(v.getContext(),SunrayActivity.class);
                        v.getContext().startActivity(intent0);
                        break;
                     case1:
                        Intent intent1=newIntent(v.getContext(),SunfeastActivity.class);
                        v.getContext().startActivity(intent1);
                        break;
                    case2:
                        Intent intent2=newIntent(v.getContext(),SunriseActivity.class);
                        v.getContext().startActivity(intent2);
                        break;

                }

            }
        });
    }

Solution 3:

I faced a similar issue. OnClick didn't seem to work.The problem was the my layout's root view was Relative Layout...So I changed that to card view (and added relative layout inside it) and it worked fine.

Solution 4:

The issue was with the way the layout was setup for the row. I made a linear layout as the top most element thus making card view as its child and everything started working.

Solution 5:

My recyclerView inside card did not respond when i click, The issue was Parent Tag of my CardView was ScrollView. In other words my row_layout i.e CardView was inside ScrollView.So i removed the ScrollView it works.

Post a Comment for "Issue With Cardview And Onclicklistener In Recyclerview"