Original URL: http://blog.csdn.net/lemon_tree12138/article/details/39337867
In what circumstances will the ListView appear misaligned? What is the reason for dislocation? How to solve? These questions are explained in the following sections.
1.ListView in what circumstances will appear dislocation?
The dislocation is that when each of our ListView's items is moved or moved out of the screen, our system will redefine the status of the item-whether it is selected, etc. Generally just show the case, our ListView rarely appears misaligned because the state has not changed.
Now we're going to take a checkbox component to illustrate, because the checkbox is a relatively basic change, and the checkbox's state change is more obvious.
For example, the following scenario:
Select No. 0 and 1th in the initial interface:
When we swipe the screen back, we'll see that the No. 0 checkbox is missing. The checkbox of the 9th one appears, which is a dislocation.
That is, when we reload the item outside the screen, the checkbox's state changes.
If so, we think there is a way to record this change and change it only when the listener hears the state that needs to be changed.
Here we use an array of arraylist<boolean> to record.
The key code is as follows:
[Java]View Plaincopyprint?
- Public View GetView (final int position, View Convertview, ViewGroup parent) {
- Viewholder viewholder = null;
- if (recordmap.get (position) = = null) {
- Convertview = Minflater.inflate (R.layout.listview_item, null);
- Viewholder = new Viewholder (Convertview);
- LOG.I (TAG, "1:" + position);
- final int finalp = position;
- Recordmap.put (position, convertview);
- ViewHolder.checkBox.setOnClickListener (new Onclicklistener () {
- @Override
- public void OnClick (View v) {
- CheckBox checkbox = (checkbox) v;
- Ischecked.set (Finalp, checkbox.ischecked ());
- }
- });
- Convertview.settag (Viewholder);
- } Else {
- LOG.I (TAG, "2:" + position);
- Convertview = Recordmap.get (position);
- Viewholder = (Viewholder) convertview.gettag ();
- }
- ViewHolder.button.setText ("click" + position);
- ViewHolder.checkBox.setChecked (Ischecked.get (position));
- return convertview;
- }
-----------------------------------------------Program source code download
"Turn" Android:listview common dislocation of the checkbox dislocation