1

I am having a View.OnClickListener in my Recycler View but whenever i click a view it does not trigger the onClick event

Here is the Code :

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView name;
    ImageView image;
    Context context;
    ArrayList<Categories_Model_Class> arrayList;

    public ViewHolder(View view, Context context, ArrayList<Categories_Model_Class> arrayList) {
        super(view);
        name = view.findViewById(R.id.category_name);
        image = view.findViewById(R.id.category_image);
        this.context = context;
        this.arrayList = arrayList;
        view.setOnClickListener(this);
        Log.i("Created", "ViewHolder");
    }

    @Override
    public void onClick(View view) {
        //Pair<View, String> pair1 = Pair.create((View) this.image, this.image.getTransitionName());
        Intent intent = new Intent(context, FullWallpaperViewActivity.class);
        //ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation((Activity)context, pair1);
        context.startActivity(intent);
        Log.i("TOUCH", "TOUCH");
    }
}

Any Suggestions?

  • Layout of the CardView Model Class

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    android:foreground="?attr/selectableItemBackground"
    android:orientation="vertical">
    
    <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_margin="4dp"
    android:clickable="true"
    android:focusable="true"
    android:foreground="?attr/selectableItemBackground">
    
    
    <ImageView
        android:id="@+id/category_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:background="@color/colorBackground" />
    
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_gradient" />
    
    <TextView
        android:id="@+id/category_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:fontFamily="sans-serif-smallcaps"
        android:text="Category!"
        android:textColor="@android:color/white"
        android:textSize="24dp" />
    
    </FrameLayout>
    </LinearLayout>
    
Yugansh Tyagi
  • 586
  • 9
  • 22

2 Answers2

5

The <FrameLayout> view inside the top layout (<LinearLayout>, accessed as view in your code) is handling the click events by themselves.

You need to remove android:clickable from your <FrameLayout> like so:

<FrameLayout
  android:layout_width="match_parent"
  android:layout_height="150dp"
  android:layout_margin="4dp"
  android:focusable="true"
  android:foreground="?attr/selectableItemBackground">

If not, it will consume the click and will not reach to the parent < LinearLayout>.

Xavier Rubio Jansana
  • 5,998
  • 1
  • 23
  • 46
0
view.setClickable(true);
view.setOnClickListener(this);
  • 3
    While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion – Michael Dodd Sep 26 '17 at 15:59
  • thanks for ur suggestion. Actually, the method names are clear to understand so that I didn't mention anything. – Shahadat Hossain Shaki Sep 26 '17 at 16:03
  • 1
    It's fine, it's just that this answer came up in the Low Quality review queue. While I may understand what the code does (makes sure the `view` in the `ViewHolder` constructor is clickable before assigning the `onClickListener`), those just starting out in Android may not know where that code goes or *why* you're using `setClickable()` – Michael Dodd Sep 26 '17 at 16:13