Androider
Androider

Reputation: 105

Click listener on a recylerview itself

I am trying to set onClick listener on a recyclerview (not the items) But it's not firing!

I have RV inside a cardview, if this matters! I searched internet; but nothing helped!

XML Code:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/text_view_entry_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/accent"
        android:background="@color/green_700"
        android:paddingTop="2dp"
        android:paddingBottom="2dp"
        android:paddingStart="8dp"
        android:textStyle="bold"
        android:paddingEnd="8dp"
        android:layout_alignParentEnd="true"
        tools:text="Entry status label text" />

    <View
        android:id="@+id/view_click_interceptor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text_view_entry_status"
        android:layout_above="@+id/relative_layout_delete_entry"
        android:layout_marginTop="16dp"
        android:background="@android:color/transparent" />


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view_entry_fields"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text_view_entry_status"
        android:layout_marginTop="16dp" />


<RelativeLayout
    android:id="@+id/relative_layout_delete_entry"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_alignParentEnd="true"
    android:layout_below="@+id/recycler_view_entry_fields">

    <ImageView
        android:id="@+id/image_view_delete_entry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_delete_black_24dp" />

    </RelativeLayout>

</RelativeLayout>

Java code (Using ButterKnife library):

@OnClick(R.id.card_view)
public void lauchFormsActivity() {
    Intent formsActivityIntent = new Intent(cardView.getContext(), FormsActivity.class);
    formsActivityIntent.putExtra(BaseActivity.PUBLIC_ID, entry.getPublicId());
    formsActivityIntent.putExtra(BaseActivity.SURVEY_ID, surveyId);
    cardView.getContext().startActivity(formsActivityIntent);
}

@OnClick(R.id.view_click_interceptor)
public void lauchFormsActivity2() {
    lauchFormsActivity();
}

Upvotes: 1

Views: 235

Answers (1)

Mehmet K
Mehmet K

Reputation: 2814

If you're ok with losing scroll and item clickability, just add a view beloew the RecyclerView

<android.support.v7.widget.RecyclerView
  android:id="@+id/recycler_view_entry_fields"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_below="@+id/text_view_entry_status"
  android:layout_marginTop="16dp"/>
<View
  android:id="@+id/view_click_interceptor"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_below="@+id/text_view_entry_status"
  android:layout_marginTop="16dp"
  android:background="@android:color/transparent"/>

and in your activity, where viewClickInterceptor is the java-object of the above view

viewClickInterceptor.setOnClickListener(new View.OnClickListener() {
  @Override public void onClick(final View v) {
    // do stuff
  }
});

Upvotes: 1

Related Questions