Adapter implementation Considerations for custom controls that ListView with multiple-selection boxes _android

Source: Internet
Author: User
Android itself provides several convenient adapter for ListView, such as Arrayadapter, Simplecurrentadapter, and so on. But in order to achieve a more complex list view and control, it is generally necessary to inherit baseadapter to achieve their own adapter.

The ListView I need is as shown in the picture. Implement SD Card Resource file browsing list, each list item consists of a ImageView, TextView, checkbox and requires that when one or more of the checkbox is selected in the entire list, The search button in the upper-right corner is displayed, otherwise it is hidden, so you need to set the listener for each list item's checkbox. If the use of Android provided by the adapter to achieve more complex, so I chose to inherit baseadapter to achieve their own adapter.

The first thing to know is the ListView display principle. After ListView receives the adapter, the ListView list item data comes from the received adapter. When ListView is to be displayed, ListView calls the adapter GetCount method to get the total number of list items to draw, and then starts calling the GetView method to load the view of each list item. That is, the list item for ListView is the view that is returned every time the GetView is invoked, and what is the view of the list item we see when we call GetView.

I inherited baseadapter to implement my own adapter, at least to rewrite the basic GetView, GetCount, GetItem, Getitemid four methods. Where the GetCount and GetView functions are as described above, so I want to implement the listener for each list item multi-marquee button, before returning view to ListView in GetView, set listeners to the multiple-selection boxes in the view. The GetView method takes three parameters to the public view getview (int position,view convertview,viewgroup parent), which is generally the view that will be the most returned by Convertview.

Here, you need a break to explain the small details of the Android system's implementation of ListView. Android Constructs a ListView list item at a time that only constructs enough list items to fit the number of screens displayed, typically around 10. When the ListView list item is more than the list item that the screen can display, the ListView can pull up and down, and the GetView method is called again to construct the view of the successor list item each time the subsequent list item is pulled. If the ListView is displayed for the first time, then the GetView argument view convertview is null; if it is the GetView that pulls the ListView call, Then the GetView parameter convertview is no longer null, but the view of the list item that has just been pulled away with the pull. The advantage of doing so is that it can save resources.

Based on this detail, if you override the GetView method to return the argument Convertview as a view, then you should determine whether Convertview is null in GetView. Empty words will need to use inflater constructs, not empty words can be used directly. I need to listen to the multiple-selection boxes in my requirements, so I need to get a Convertview control in Convertview and set up the listener before I return to the box.

At first, I thought it would be able to achieve my needs, but the results came out unexpectedly. When I click on a multiple selection box, the list is pulled down, and the list box that comes out below is also turned into a checked state. Notice that every time I click on a multiple-selection box, the distance between the selected boxes is constant, and 11 items are always separated. Then, recalling the characteristics of the Convertview parameters in GetView, when I pulled down, the ListView call GetView method of Convertview is recycled because the pull is hidden view. In my case, because the multiple-selection box is a state-marked control, my GetView does not reset its state, so this is causing this strange phenomenon. My solution is to create a Boolean array in the adapter class that I implement to hold the state of the multiple marquee of the corresponding list item (the first argument in getview position is the list item ID, which is identified by the data, not by the list item view. Therefore, the list item data can be checked and unchecked by position, and each call GetView determines the Boolean value at the position position to determine the status of the multiple-selection box.

Similarly, based on this principle, the use of other stateful controls also requires attention to the problem of GetView recycling. Of course, you can also not use the Convertview most getview return results, and in GetView each call to reconstruct a view, or the adapter class to construct a view array with the number of data as long. But doing so will be more of a drain on resources.

In addition, the GetItem and Getitemid methods in Baseadapter are not used in the ListView construction process, but are said to be invoked in some listeners about ListView. So it's best to return a meaningful value to both methods when inheriting baseadapter. Getitemid generally returns the corresponding Position,getitem returns the list data object corresponding to the position.

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.