6

I use a RecyclerView that shows a list of entries. Each entry hosts another RecyclerView that is a list of images.

I now want to make this nested RecyclerView clickable, not the items of it, but the whole view.

How can I achieve that?

Problem:

  • setting the onClickListener for the main view of the nested RecyclerView does work, but only if I click outside the RecyclerView itself
  • clicking on the nested RecyclerView does not hand on the clicks to the parent view (I even tried to set clickable, focusable, focusableIntouch to false, still, the touch is not delegated but consumed by the nested RecyclerView...

Here's the view for the wrapping adapter:

<?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"
    card_view:cardCornerRadius="0dp"
    android:layout_margin="5dp"
    android:foreground="?android:attr/selectableItemBackground" >

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

        <RelativeLayout
            android:id="@+id/rlTop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="Datum"
                android:textStyle="bold"
                android:id="@+id/tvDate" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="Info"
                android:id="@+id/tvInfo" />
        </RelativeLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvData"
            android:clickable="false"
            android:focusableInTouchMode="false"
            android:focusable="false"
            android:layout_below="@+id/rlTop"
            android:layout_width="match_parent"
            android:layout_height="68dp"
            android:scrollbars="horizontal" />

    </RelativeLayout>

</android.support.v7.widget.CardView>
prom85
  • 14,805
  • 15
  • 98
  • 206
  • Do u want the inner recycler view to be horizontal? – EE66 Jun 10 '15 at 17:03
  • Yes. It shows a row of pictures... – prom85 Jun 10 '15 at 17:04
  • I didnt fully understand. Do u want to have some kind of onItemClickListener? for the entire row? – EE66 Jun 10 '15 at 17:06
  • Exactly... I want the entire row to be clickable. Setting an `onClickListener`to the row works, but the area consumed by the nested `RecyclerView` does not react, only the rest of the view... – prom85 Jun 10 '15 at 17:09

3 Answers3

4

I looked into RecyclerView source and this helped:

recyclerView.setLayoutFrozen(true)
David Vávra
  • 16,232
  • 7
  • 43
  • 53
1

This is an old question, but i have the same task and didn't find good solution. I just placed the transparent view over recyclerview and set click listener to this view.

<android.support.v7.widget.CardView
    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:orientation="vertical"
    app:cardBackgroundColor="@color/white"
    app:cardCornerRadius="0dp">

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

        <LinearLayout
            android:id="@+id/dataLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="@dimen/history_margin_v"
            android:paddingEnd="@dimen/history_margin_h"
            android:paddingLeft="@dimen/history_margin_h"
            android:paddingRight="@dimen/history_margin_h"
            android:paddingStart="@dimen/history_margin_h"
            android:paddingTop="@dimen/history_margin_v">

            <TextView
                android:id="@+id/day"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/widget_margin_v"
                android:textColor="@color/font_blue"
                android:textSize="@dimen/font_size_large"/>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/images"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>
        <View
            android:id="@+id/clickView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/selectableItemBackground"
            android:layout_alignLeft="@+id/dataLayout"
            android:layout_alignRight="@+id/dataLayout"
            android:layout_alignStart="@+id/dataLayout"
            android:layout_alignEnd="@+id/dataLayout"
            android:layout_alignTop="@+id/dataLayout"
            android:layout_alignBottom="@+id/dataLayout"/>
    </RelativeLayout>
</android.support.v7.widget.CardView>
Alexey Rogovoy
  • 671
  • 6
  • 12
0

In order to do the entire row clickable and not every image u need to implement a custom onItemClickListener (name comes from listview , sorry). Take a look at this link this worked out perfectly for me.

Edit:

The nested recycler steals your clicks. in order to fix that u will need to create a cusom touch listener and pass it to the nested recycler. Just like the one that u put to the outer ercycler but pass the event to the outer.

Edit 2:

Add a selector with two states just like a button to the entire row, then at the onclick on the inner recycler setSelected(true) the row and postDelay with 50ms for setSelected(false) this will give the "click" effect.

Community
  • 1
  • 1
EE66
  • 4,377
  • 1
  • 15
  • 20
  • This makes the row clickable, but only the area not consumed by the nested `RecyclerView`... – prom85 Jun 10 '15 at 17:15
  • To your edit: that's a work around I'm currently using... Drawback; clicks on the nested `RecyclerView` don't give a visual feedback... The row should give the click feedback if I click on the nested `RecyclerView` as well... – prom85 Jun 10 '15 at 17:39