Reputation: 7980
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
<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:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/unit_5"
android:clickable="true"
android:longClickable="true"
card_view:cardCornerRadius="@dimen/unit_5">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/spinnerWidth"
android:background="@drawable/row_item_background"
android:clickable="true"
android:orientation="vertical"
android:padding="@dimen/dialog_left_padding">
<!-- Other items -->
</LinearLayout>
</android.support.v7.widget.CardView>
Following are my adapter and ViewHolder
private class SampleAdapter extends RecyclerView.Adapter<SampleViewHolder> {
ArrayList<Item> arrayList;
private LayoutInflater inflater;
public SampleAdapter(Context context) {
inflater = LayoutInflater.from(context);
this.arrayList = new ArrayList<>();
arrayList.addAll(items);
}
@Override
public SampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.layout1, parent, false);
SampleViewHolder holder = new SampleViewHolder(view);
holder.setClickHandler(handler);
return holder;
}
@Override
public void onBindViewHolder(SampleViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return arrayList.size();
}
}
static class SampleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
TextView name;
IClickHandler clickHandler;
public IClickHandler getClickHandler() {
return clickHandler;
}
public void setClickHandler(IClickHandler clickHandler) {
this.clickHandler = clickHandler;
}
public SampleViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView
.findViewById(R.id.name);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
@Override
public void onClick(View view) {
if (getClickHandler() != null) {
getClickHandler().onItemClicked(view, getLayoutPosition());
}
}
@Override
public boolean onLongClick(View view) {
if (getClickHandler() != null) {
getClickHandler().onItemLongClicked(view, getLayoutPosition());
}
return true;
}
}
But this onClick is never fired.
However, if I replace CardView with LinearLayout every thing works fine. I am not sure what is the issue here. Can somebody help me in this?
Thanks.
Upvotes: 3
Views: 10074
Reputation: 373
I was having the same issue . In my case, the problem was that i had set
android:clickable="true"
android:focusable="true"
for a relative layout which was inside my Cardview.
Hence the click listeners on cardview and the whole itemView were not getting triggered.
Wasted 4 hours there.
Upvotes: 0
Reputation: 141
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
public void onBindViewHolder(View_Holder_For_Grocery holder, final int position) {
holder.cv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (position){
case 0:
Intent intent0=new Intent(v.getContext(),SunrayActivity.class);
v.getContext().startActivity(intent0);
break;
case 1:
Intent intent1=new Intent(v.getContext(),SunfeastActivity.class);
v.getContext().startActivity(intent1);
break;
case 2:
Intent intent2=new Intent(v.getContext(),SunriseActivity.class);
v.getContext().startActivity(intent2);
break;
}
}
});
}
Upvotes: 2
Reputation: 1
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.
Upvotes: 0
Reputation: 93
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.
Upvotes: 1
Reputation: 7980
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.
Upvotes: 0
Reputation: 9050
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:
static class SampleViewHolder extends RecyclerView.ViewHolder {
TextView name;
public SampleViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
}
}
And then in SampleAdapter
@Override
public SampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.layout1, parent, false);
SampleViewHolder holder = new SampleViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(SampleViewHolder holder, int position) {
final Item item = arrayList.get(position);
holder.name.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
//notice I implemented onClickListener here
// so I can associate this click with final Item item
}
});
}
Upvotes: 7