Problems with Clicklistener in RecylerView

38 Views Asked by At

I have a recyclerview which uses ViewBinding for the rows. I have attached a listener to the root object of the row views.

The weird thing is that it only recognises the click if it on somewhere where there is text. So, if the row has only a small amount of text on the line it won't recognise the click further along the line.

This is my row layout:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:paddingTop="2dip" android:paddingBottom="2dip" android:paddingLeft="10dp"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:mode="twoLine" >
    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:layout_marginLeft="2dp"
        android:gravity="bottom"
        android:text="Title"
        android:textAppearance="?android:attr/textAppearanceLarge"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        tools:layout_editor_absoluteX="16dp" />

    <TextView android:id="@id/summary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_alignStart="@id/title"
        android:layout_marginTop="2dp"
        android:layout_marginLeft="2dp"
        android:textAppearance="?attr/textAppearanceListItemSecondary"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/title"
        android:text="Summary"/>

</androidx.constraintlayout.widget.ConstraintLayout>

The code that uses it looks like this:

val binding = WefiSettingsSimpleListItem2Binding.inflate(layoutInflater)
binding.root.setOnClickListener(settingsItemClickListener)

The click listener only gets called if I click on areas in the "title" or "summary" fields where there is actually text. Any click beyond the text is ignored. This is an example of how the screen looks.

enter image description here

1

There are 1 best solutions below

2
gabindw On

Your item +id/summary and +id/title need

layout_constraintRight_toRightOf="parent" ?