How does Android achieve full list selection?

Source: Internet
Author: User

How does Android achieve full list selection?

Background:In the list of "Shopping Cart", "my attention", and "shipping address, often, the UI Designer adds a "select all" checkbox (called its parent checkbox) to the outer Activity, and the subcheckbox exists in every item in the list, that is, listitem, while listitem is often written in an adapter. How can I transfer the internal and external layers, that is, the inner adapter and the outer Activity?

The outer Activity is passed to the inner adapter:

In this example, you need to check the parent checkbox control of the outer Activity, and mark all the child checkboxes of all items in the inner adapter as the check status. How? First, write a chooseAll (boolean flag) function in the adapter, and then call it in the outer Activity. The content of the function is to traverse all sub-items and modify each sub-checkbox in turn. Do not forget to refresh the adapter in notifyDatasetChanged in the function.


The inner adapter transmits the following information to the outer Activity:

Strictly speaking, the adapter cannot control the outer Activity. The Activity can call the function of the adapter and control the Refresh. The inner adapter cannot control the loading of the outer Activity. Here, I also used a more clever method to solve my practical problems. In this example, we need to modify the outer checkbox Based on the subitem item checkbox of the inner adapter. That is, if you click the last unselected check box, the outer checkbox needs to be selected accordingly. Because it is not convenient to enable global variables, or even to enable the thread to listen at any time, we use a clever method to pass the context of the outer Activity as a parameter of the adapter constructor, then, get the parent checkbox through context, and modify the last checkbox after the adapter selects the last one. How can I obtain an external control through context? The following sentence is used for forced transformation.

Adapter. this. chooseAllImg = (ImageView) (Activity) mContext). findViewById (R. id. chooseAll );

In this way, the adapter can modify this Img to modify the outer Img, because both the internal and external Img point to the same resource file, that is, R. id. chooseAll.


The problem arises again:

This only modifies the checkbox appearance in the outer Activity, but is not modified logically. If the flag of the outer Activity is set to false at the beginning, and the subitem is selected in turn until all items are selected, the adapter modifies the checkbox image of the outer Activity and changes it to the selected image, however, the partial variable of the Activity itself-the fully-selected flag is still false, because the adapter cannot obtain the partial variable of the Activity through context. If the Activity needs to use this flag to make some judgments, then the logic will be disconnected. It looks like all the selected images, but the flag is still false. So my countermeasure is: Do not use this flag for Activity. What is it? The only thing that can maintain communication is the small checkbox Img. Coincidentally, Android controls can implement a Tag and bind it to each control. Then, I only need to set the Img Tag to true or false to replace this flag. How to Set tags? It is very simple. You only need to set the parameter setTag (true) when the entire layer is selected, and then getTag () when the flag is used in the outer layer. Of course, getTag must be converted to the Boolean type.


How to implement full selection (ctrl + a) in the List control can be answered

Determines whether the focus is on the List control,
If yes, all items can be selected by setting all items highlighted.
If you need to perform other operations not only for such a simple visual effect,
You have to handle it separately.
Press sz_04022 to select all items in the list. You only need to process the ctrl + a message on the keyboard when the focus is on the list.

In Android, how does one change the background of the selected ListView item?

It can be fully implemented, which uses the Android Selector (display the picture corresponding to this status based on the component status as the display background ).
Save the following XML file as your own name. xml file (such as list_bg.xml). Note that this file is equivalent to a background image selector. When used by the system, the corresponding background image is used according to the status of the list items in the ListView, I will describe the usage and images below. Also, you can think of it as an image and put it in the drawable directory. Configure the background attribute android: background = "@ drawable/list_bg" to achieve your goal.

<? Xml version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: android = "schemas.android.com/apk/res/android">
<Item android: state_window_focused = "false"
Android: drawable = "@ drawable/background of the image without focus"/>
<Item android: state_focused = "true" android: state_pressed = "true"
Android: drawable =
"@ Drawable/obtain the background image when the focus is clicked in non-touch mode"/>
<Item android: state_focused = "false" android: state_pressed = "true"
Android: drawable = "@ drawable/background image when you click in touch mode"/>
<Item android: state_selected = "true"
Android: drawable = "@ drawable/background of the selected image"/>
<Item android: state_focused = "true"
Android: drawable = "@ drawable/background of the image when the focus is obtained"/>
</Selector>

----------------------------------------------------------------
You can see the default Implementation of the source code ListView list item background
The SDK directory \ tools \ lib \ res \ default \ drawable \ list_selector_background.xml is used to modify the desired effect. Hope to be useful to you ....... Remaining full text>

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.