Android: descendantFocusability usage analysis,

Source: Internet
Author: User

Android: descendantFocusability usage analysis,

A common problem in development is that the listview in a project is not just a simple text, but you often need to define your own listview, and your own Adapter will inherit the BaseAdapter and write it as needed, the problem may occur. When you click an item, the system does not respond and cannot obtain the focus. Most of the reasons are that sub-controls such as ImageButton, Button, and CheckBox exist in your own defined items (also called the sub-Control of Button or Checkable ), at this time, these child controls will get the focus, so when you click an item, the Child control is changed, and the click of the item itself has no response.

In this case, descendantFocusability can be used to solve the problem. The API description is as follows:

Android: descendantFocusability

Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.

Must be one of the following constant values.

 

This attribute defines the relationship between the viewGroup and its child controls when the view gets the focus.

There are three types of attribute values:

BeforeDescendants: viewgroup takes precedence over its subclass control and obtains the focus.

AfterDescendants: viewgroup obtains the focus only when its subclass control does not need to obtain the focus.

BlocksDescendants: viewgroup overwrites the subclass control and directly obtains the focus.

 

We usually use the third type, that is, addAndroid: descendantFocusability = "blocksDescendants"So far, the history event clicked by listview has come to an end. Experience: In addition to querying materials on the Internet, you can also try the functions of each attribute, read more official documents (I always think it is better to read the original documents than to translate them ).

<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: background = "@ color/background">


<RelativeLayout
Android: id = "@ + id/rel_group"
Android: layout_width = "fill_parent"
Android: layout_height = "60dip"
Android: layout_toLeftOf = "@ + id/linear_view"
Android: background = "@ drawable/list_selector"
Android: descendantFocusability = "blocksDescendants"
>


<ImageView
Android: id = "@ + id/iamge_group_photo"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentLeft = "true"
Android: layout_centerVertical = "true"
Android: layout_marginLeft = "10dip"
Android: background = "@ drawable/btn_group_change_normal"
Android: contentDescription = "@ string/app_name"/>


<! -- Display group name and contact count -->


<LinearLayout
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerVertical = "true"
Android: layout_marginLeft = "10dip"
Android: layout_toRightOf = "@ + id/iamge_group_photo"
Android: orientation = "vertical">


<TextView
Android: id = "@ + id/txt_group_name"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textColor = "#000000"
Android: maxEms = "8"
Android: singleLine = "true"
Android: ellipsize = "end"
Android: textSize = "17sp"/>


<TextView
Android: id = "@ + id/txt_contact_count"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "4dip"
Android: textSize = "14sp"/>
</LinearLayout>
</RelativeLayout>


<LinearLayout
Android: id = "@ + id/linear_view"
Android: layout_width = "0.5dip"
Android: layout_height = "50dip"
Android: orientation = "vertical"
Android: layout_centerVertical = "true"
Android: layout_toLeftOf = "@ + id/rel_delete"
Android: background = "@ drawable/list_item_divide_line"/>


<! -- Delete group button -->


<RelativeLayout
Android: id = "@ + id/rel_delete"
Android: layout_width = "60dip"
Android: layout_height = "60dip"
Android: layout_alignParentRight = "true"
Android: background = "@ drawable/list_selector"
Android: descendantFocusability = "blocksDescendants">


<ImageView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerInParent = "true"
Android: background = "@ drawable/delete_groupmember_icon_selector"
Android: contentDescription = "@ string/app_name"/>
</RelativeLayout>


</RelativeLayout>



After setting android: descendantFocusability = "blocksDescendants", the onItemClickListener still does not work.

Set disable to false. Focusable only prevents it from getting focus, and its click event is still available. The two are substantially different. The focus you set is false. At most, it can be effective in remote control mode or when you press and hold your hands away. I don't know what features you want.

In Android development, how does one use Intent?

There are too many Intent things to say. If you are a beginner, you only need to understand its explicit calls. For example, you need to jump from activity1 to activity2 and execute the following statement in activity1, you can jump to activity2, and do not forget to go to manifest. define activity2 in xml.
Intent intent = new Intent (activity1.this, activity2.class );
StartActivity (intent );
This is the simplest way, of course, this method can also transmit data (from activity1 to activity2), such
Intent intent = new Intent (activity1.this, activity2.class );
Intent. putExtra ("name", "value ");
StartActivity (intent );
Then, the onResume or onCreate method is called to obtain the Intent in the location received in activity2.
Intent getIntent = getIntent ();
Bundle bundle = intent. getExtras ();
String va = bundle. getString ("name ");

The above is the basic usage of explicit calling. The essence of Intent is implicit calling. If you have any questions, please contact us.

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.