2

I have a LinearLayout with a nested RecyclerView showing a list of items. I'd like to open a popup when RecyclerView is clicked (either one of the items or the background white area), but the usual setOnClickListener is not working.

Of course I can put a click listener on each of the items, but the white area between them remains unclickable.

Is there a way to make the entire RecyclerView area clickable?


EDIT: I've added some sample code. I'd like to have the entire layout clickable to open a popup, but while the first three views behave properly, the RecyclerView does not.

<LinearLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/spacing_half"
        android:background="@color/color_item_margin_divider"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/fragment_tags_title"
        style="@style/ItemFragmentHeader"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/spacing_line"
        android:background="@color/color_line_divider"/>
    <RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/spacing_half"/>
</LinearLayout>
Davide Cannizzo
  • 2,826
  • 1
  • 29
  • 31
Alessandro
  • 3,666
  • 2
  • 28
  • 41

2 Answers2

0

Add onClickListener in the viewHolder . Below is a snippet of my project where I had implemented Listener

public class MyViewHolder extends RecyclerView.ViewHolder {
       public ImageView shotThumbnail;
       public MyViewHolder(View view) {
                    super(view);
                    shotThumbnail = (ImageView) view.findViewById(R.id.shotThumbnail);
                    shotThumbnail.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            //Put here stuff that you want the onclickListener should do 

                        }
                    });

Check if this link helps: Detect click on RecyclerView outside of items

Community
  • 1
  • 1
Panda2109
  • 39
  • 10
0

You should use padding instead of margin.


    <RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_padding="@dimen/spacing_half"/>

Cause whenever you use padding it will crop your RecyclerView. But whenever you use padding it will just shorten your inside functions. hence you need padding. Then you should use an onClickListener in MainActivity.

recyclerView.setOnClickListener(v->{
  //Do whatever you want
});

Unknown
  • 23
  • 7