Adapter implements the considerations for ListView custom controls with multiple checkboxes and other states

  • 2020-05-09 19:14:24
  • OfStack

Android itself provides several convenient Adapter for ListView, such as ArrayAdapter, SimpleCurrentAdapter, and so on. However, in order to implement more complex list views and controls, 1 generally inherits BaseAdapter to implement its own Adapter.

I need ListView is the realization of the sample as shown in figure 1 SD card resource file browsing list, each list item consists of 1 ImageView, TextView, CheckBox, and when the entire list of more than 1 or 1 Checkbox is selected, it is displayed in the top right corner of the search button, or hidden, so you need to set CheckBox listener for each list item. The implementation of Adapter provided by Android is complicated, so I choose to inherit BaseAdapter to implement my own Adapter.

The first thing to know is the ListView display principle. After ListView receives Adapter, the list item data for ListView is derived from the received Adapter. When ListView is presented, ListView calls Adapter's getCount method to get how many list items 1 needs to draw, and then calls getView to get the View for each list item to load. That is to say, the list item of ListView is the View returned each time getView is called, and the list item of View we see each time getView is called is what View looks like.

I inherited BaseAdapter to implement my Adapter, so I had to rewrite the basic getView, getCount, getItem and getItemID methods at least. The functions of getCount and getView are described above, so I need to set the listener to the multiple marquee buttons in View before returning View to ListView in getView. The getView method takes three parameters, public View getView (int position, View convertView, ViewGroup parent). Generally, 1 is the View that returns convertView.

In this case, you need to insert a note 1 to explain the implementation details of Android system to ListView. When Android constructs ListView list items, it only constructs enough list items at a time to satisfy the number displayed on the screen. 1 is usually about 10 items. When ListView has more list items than the screen can display, ListView can pull up and down, and each time it pulls a subsequent list item it calls the getView method again to construct the View for the subsequent list item. If ListView is displayed for the first time, the getView parameter View convertView is empty null; If it is getView pulling the ListView call, then the getView parameter convertView is no longer null, but View pulling the list item that has just been pulled away from the hidden list. The advantage of doing this is that you save resources.

Based on this detail, if the getView method is overwritten with the parameter convertView as returning View, then getView should determine whether convertView is null. If it is empty, it needs to be constructed using Inflater. If it is not empty, it can be used directly. I need to listen for multiple checkboxes in my requirement, so I need to get the multiple checkbox control in convertView and set the listener before returning to convertView.

At first, I thought this would fulfill my needs, but the results were unexpected. When I click on one of the multiple checkboxes, I pull down the list, and the unselected list box becomes selected. Notice that each time I click on a multiple marquee and then pull it down, the distance between the selected multiple marquee is the same, always 11 items apart. So, recall the characteristics of the convertView parameter in getView, and when I pull down, ListView calls convertView in the getView method, which is the View that is recycled and hidden by pulling. In my example, since the checkbox is a control with a status flag, my getView did not reset its state, which caused this strange phenomenon. My solution is realized in my Adapter class to create a boolean array is used to save the state of the corresponding checkbox list item (getView ID position first parameter is the list of items in a, is based on the data to identify, not according to the list item View to identify, so can the list item data are selected according to position and non selected logo), in which every call getView judgment position position boolean value to determine boxes.

Similarly, based on this principle, the use of other stateful controls also requires attention to the issue of getView recycling. Of course, instead of using convertView as the return result of getView, you can construct an View array as long as the number of data in getView with each call, or in Adapter class. But it's a lot of resources to do that.

In addition, the getItem and getItemId methods in BaseAdapter are not used in the construction of ListView, but they are said to be called in some listeners about ListView, so it is better to return a meaningful value to these two methods when inheriting BaseAdapter. getItemId1 returns the corresponding position, getItem returns the list data object corresponding to position.

Related articles: