Android Puzzle------------A ListView that contains button buttons, the list item cannot get focus

Source: Internet
Author: User

Problem Description:

A friend who has added a button to a ListView list item will not be unfamiliar with this issue, and our demo will appear at run time----------can get button click events in the list item, but when we want to get a click event for the entire list item, You will find that clicking on the list item at this time has no response. Why?

The reason is that when a widget with a "click" event is added to the ListView, the ItemClick event of the ListView will be masked by the Click event of the other widget . and cannot be triggered.

Let's look at the XML file for the list item first

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "@drawable/border_corner"android:descendantfocusability= "Blocksdescendants"android:gravity= "Center"android:orientation= "Horizontal" > <!--Beforedescendants:viewgroup takes precedence over its subclass control and gets to focus Afterdescendants:viewgroup gets focus only if its subclass control does not need to get focus Blo Cksdescendants:viewgroup overrides the subclass control and gets the focus directly-<ImageView Android:id= "@+id/img_icon"Android:layout_width= "50DP"Android:layout_height= "50DP"android:contentdescription= "Contact Avatar"android:src= "@drawable/ic_launcher"/> <LinearLayout android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_marginleft= "3DP"Android:layout_weight= "1"android:orientation= "Vertical" > <TextView Android:id= "@+id/tvcontactname"Android:layout_width= "Wrap_content"Android:layout_height= "25DP"Android:text= "Xiao Ming"android:textsize= "20sp"/> <TextView Android:id= "@+id/tvcontactnumber"Android:layout_width= "Wrap_content"Android:layout_height= "15DP"Android:text= "11111111111"/> </LinearLayout><!--android:focusable= "false"set to not get focus for the control-<Button Android:id= "@+id/btncall"Android:layout_width= "35DP"Android:layout_height= "35DP"Android:background= "@drawable/bg_btn_call"android:contentdescription= "Call"android:focusable= "false"/> <Button Android:id= "@+id/btnsendmessage"Android:layout_width= "35DP"Android:layout_height= "35DP"Android:layout_marginleft= "3DP"Android:layout_marginright= "5DP"Android:background= "@drawable/bg_btn_message"android:contentdescription= "Texting"android:focusable= "false"/></linearlayout>

Did you find anything special?

Workaround One:

Do you see what's Inside the box?

Android:descendantfocusability= "Blocksdescendants"

The function of the Descendantfocusability property is to set the relationship between the ViewGroup and its child controls when the focus is taken for a view. The value of the property is as follows:

Beforedescendants:viewgroup takes precedence over its subclass control and gets to focus

Afterdescendants:viewgroup gets focus only if its subclass control does not need to get focus

Blocksdescendants:viewgroup overrides the subclass control and gets the focus directly

Of these three properties, the most common is the 3rd, that is, the item layout on the root layout to add this property, so that you can correctly get to the list item.

Workaround Two:

At the beginning of the article, we mentioned that when a widget with a "click" Event is added to the ListView button, the ItemClick event of the ListView will be masked by the Click event of the other widget. and cannot be triggered.

So here's how it's solved:

Such as:

Android:focusable= "false"

When the button's Focusable property is set to False, the control is no longer able to get the focus, so naturally it is not possible to block out the ItemClick event of the ListView.

Personally, Method 2 and Method 1 have a certain similarity, all in the ListView in the item gets the focus, thereby triggering the ItemClick event. If there are any different views, please leave a message and a small series of discussion. (*^__^*)

Information Link:

Link 1:http://blog.csdn.net/gyflyx/article/details/6567701

Link 2:http://mobile.51cto.com/android-265197_3.htm

Android Puzzle------------A ListView that contains button buttons, the list item cannot get focus

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.