2

I have a ListView with a custom Adapter which populates Items with an EditText and a TextView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">

    <EditText
        android:id="@+id/et_choice_prob"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberSigned"/>

    <TextView
        android:id="@+id/tv_choice_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/et_choice_prob"
        android:layout_alignBaseline="@id/et_choice_prob"
        android:layout_marginLeft="5dip"
        />


</RelativeLayout>

If I want to set an OnItemClickListener it gets never called, due to the EditText stealing the focus.

Another thread mentioned that I should set focusable to false, which will remove the ability to actually edit the TextView.

I have also tried to set descendantFocusability of the ListView to any available option withot success.

The Adapter is doing nothing fancy, but I'll provide source on request. Everything else is done in the same way on another ListFragment and works.

Rafael T
  • 15,401
  • 15
  • 83
  • 144

3 Answers3

1

Try adding this code in yout EditText tag

android:focusable="true"
android:focusableInTouchMode="true"
Jas
  • 3,207
  • 2
  • 15
  • 45
0

Set setOnClickListener for RelativeLayout while inflating list item views in getView method of your adapter. Your EditText does not match with parent so this must be work when you click other side of list row.

faranjit
  • 1,567
  • 1
  • 15
  • 22
  • actually same solution as in the first comment on the question from @JulienGenoud – Rafael T Aug 05 '16 at 10:48
  • After searching in SO, I think this should be the proper way to solve this problem, though it has too much boilerplate code here. But any other solution like `descendantFocusability` didn't work for me. Besides, I can't understand why `focusable` may interrupt `onItemClickListener` in listview. – Jian Guo Feb 24 '20 at 02:56
0

Set onTouchlistener on view of individual item in adapter class. Also you have to manage the state of list such that when list is being scrolled, onTouch should be disabled and when not scrolled, onTouch should work as usual. It works fine on recycler view in my project. Should work with ListView as well.

Zealous System
  • 2,080
  • 11
  • 22