How Android implements the full list selection effect

Source: Internet
Author: User

background: When it comes to listing issues such as "Shopping cart" and "My Concerns," "Shipping Address," the UI designer will often add a checkbox for the "Select All" function to the outer activity (call his parent checkbox). A sub-checkbox exists in each item in the list, that is, ListItem, and ListItem is often written in a adapter. How to complete the internal and external layer, that is, the inner layer of adapter and outer activity transfer it?

The outer activity transfers to the inner layer adapter:

In this example, the parent CheckBox control for the outer activity is checked to mark all the sub-checkboxes of all item in the inner adapter as checked. What do you do? First Write a Chooseall (Boolean flag) function in adapter, and then invoke the outer layer activity. The content of the function is to iterate through all the children to modify each sub-checkbox in turn. Finally, do not forget to notifydatasetchanged in the function, to refresh the adapter.


The inner layer of the adapter is transferred to the outer activity:

Strictly speaking, adapter is unable to control the outer activity, activity can call adapter function, control refresh and so on, the inner layer adapter is completely unable to control the loading of the outer activity. Here I also used a more trickery method to solve the practical problems I faced. In this case, what I need to do is to modify the Outer checkbox based on the sub-checkbox of the adapter sub-item in the inner layer, that is, if you click the last unchecked check box, the outer checkbox needs to be selected accordingly. Because it is inconvenient to open the global variable, even the thread at any time to listen, so the trickery way, the outer activity of the context as a adapter constructor parameters passed in, and then through the context to obtain the parent checkbox, Modify the checkbox accordingly after adapter selects the last item. How do I get external controls through the context? Use the following sentence to force the transformation.

ADAPTER.THIS.CHOOSEALLIMG = (ImageView) (Activity) mcontext). Findviewbyid (R.id.chooseall);

This allows adapter to modify the outer img by modifying the IMG, since both the inside and outside IMG point to the same resource file, the R.id.chooseall.


The problem arises again:

This only modifies the appearance of the checkbox in the outer activity, which is not logically modified. If the first outer activity is completely selected flag is false, then the subkey is selected until all selected, then adapter will modify the outer activity of the checkbox picture, instead of the selected picture, but the activity itself local variables--Select all flag is still false, Because adapter is a local variable that cannot get activity through the context. If the activity next needs to make some judgments through this flag, that logic will not go through, clearly appears to be all the selected pictures, but flag still write is false. So my strategy is: Do not use this flag for activity. With what? The only thing that can keep the communication is this little checkbox Img, and the trickery is that the Android control can implement something called tag that binds to each control. Well, I just have to set the IMG tag to be true instead of this flag. How is the tag set? It is simple to Settag (true) when the inner layer is fully selected, and then Gettag () when the outer layer is used with flag. Of course Gettag needs to be converted to a Boolean type.

How Android implements the full list selection effect

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.