Android custom ListView click event invalid

Keywords: Android xml encoding

Because the built-in listview cannot meet the needs of the project, implement its own Adapter to inherit the ArrayAdapter to implement the Item project of the custom listview.

Each item that appears and clicks on the ListView will not execute the onItemClick method in the setOnItemClickListener.

The reason is that there are some sub controls in the item. By default, the focus obtained by clicking runs away from the sub control, and the click fails.

terms of settlement:

Add android:descendantFocusability="blocksDescendants" to the root directory of the item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:padding="5dp">


        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/message_oc" />

        <TextView
            android:id="@+id/textTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="title"
            android:textSize="25dp"
            android:layout_marginLeft="15dp"/>

        <TextView
            android:id="@+id/textDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="date" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textMultiLine"
            android:text="message"
            android:textSize="20dp"/>
    </LinearLayout>

</LinearLayout>

 

This property defines the relationship between the viewGroup and its child controls when one gets the focus for the view.

There are three values for the property:

beforeDescendants: viewgroup takes precedence over its subclass controls to get focus

afterDescendants: viewgroup gets focus only when its subclass control does not need to get focus

Blocksdedendants: viewgroup will override the subclass control and get the focus directly

 

We use the blocksdedendants property to override the class control and get the focus directly.

Posted by phphunger on Fri, 31 Jan 2020 14:57:21 -0800