0

I have a ListView that contains an imageview, a textview, and a checkbox. The rows became unclickable when I added the imageview. I have tried many solutions I found including android:focusable="false", android:descendantFocusability="blocksDescendants" and android:clickable=true.

Here is my ListView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:clickable="true"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical" >
<ImageView
    android:id="@+id/imgListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:focusable="false"
    android:src="@drawable/ic_launcher" />
<TextView
    android:id="@+id/txtListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@id/imgListView"
    android:focusable="false"
    android:textColor="#FFFFFF" />
<CheckBox
    android:id="@+id/chkListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:focusable="false" />
</RelativeLayout>

and here is the activity using it:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".FolderActivity" >
<ListView
    android:id="@+id/lstFolders"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" 
    android:dividerHeight="1dp"
    android:divider="#FFFFFF">
</ListView>
</RelativeLayout>

I just need the rows to be clickable again like they were before.

PS: I alreadu had the same problem when I first added the checkbox and solved it with android:focusable="false".

LedZep
  • 75
  • 1
  • 1
  • 8

2 Answers2

1

We need the following changes to allow item click on listview to work by avoiding click/focus on the components of the listview items.

  1. Add android:descendantFocusability="blocksDescendants" on the item layout.
  2. Add android:focusableInTouchMode="false" on the components inside listview item(example, imageview, button, checkbox, textview, custom view, canvas)
  3. Return false on the event listners on the custom view (example: class that extends View implements View.OnTouchListener and the method public boolean onTouch(View v, MotionEvent event) should return false.
Bruce
  • 793
  • 8
  • 17
0

Why don't you write the on click listener for each of these views inside your adapter's getview method. also refer this question
Android: ListView elements with multiple clickable buttons.
As even i had had the same issue and in my experience that xml parameter works if you have only one element that is clickable in listview, for multiple ones i wrote it inside the adapter only.

Community
  • 1
  • 1
Aniruddha K.M
  • 7,361
  • 3
  • 43
  • 52