Search code examples
androidlistviewparent-childselected

android: how to disable selected and pressed attributes of parent view in listView


I have a listView with custom rows containing buttons and I populate the listView with a custom cursorAdapter. I have set a selector for the button to change the background of it when pressed and it works great when I press on the button. The problem is it also works when I press anywhere else on the row.

I tried to set the parent view's .setPressed, .setSelected, .setFocusable, .setFocusableInTouchMode, .setClickable attributes to false but it did not work.

I also tried to set button's .setDuplicateParentStateEnabled to false but it did not help either.

If you can help me I will be grateful,

Thank you in advance

Let me extend my question: I have the rows without the button (which is the TextView with id of "delete_button" in xml code below) first. When I click another button in the view, the delete buttons slide into the rows and the background of the other TextView (id:list_title_text) changes to a constant color (because I don't want it to be clickable anymore)

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/title_rows_relativelayout"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TextView
        android:id="@+id/list_title_text"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:textSize="25dp"
        android:textColor="@android:color/white"
        android:layout_alignParentRight="true"
        android:background="@drawable/selector_for_list_rows"
        android:paddingLeft="15dp"
        android:paddingBottom="5dp"
        android:paddingRight="15dp"
        android:gravity="center_vertical"
        android:typeface="normal" />
    </TextView>
    <TextView 
    android:id="@+id/delete_button"
    android:background="@drawable/selector_for_delete_button"
    android:layout_width="wrap_content" 
    android:layout_height="40dp"
    android:textSize="23dp"
    android:textColor="@android:color/white"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:gravity="center"
    android:layout_alignTop="@+id/row_icon_background" 
    android:layout_alignParentRight="true"
    android:layout_marginRight="5dp">
    </TextView>
 </RelativeLayout>

Solution

  • I solved this by setting the button's setFocusable attribute to true inside my custom ListAdapter (I changed my CursorAdapter to ListAdapter after I wrote the question but I am sure it will work with CursorAdapter too). Now the button background changes only when I click on the button and nothing happens when I click anywhere else.