"Turn" Android:listview common dislocation of the checkbox dislocation

Source: Internet
Author: User

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?
  1. Public View GetView (final int position, View Convertview, ViewGroup parent) {
  2. Viewholder viewholder = null;
  3. if (recordmap.get (position) = = null) {
  4. Convertview = Minflater.inflate (R.layout.listview_item, null);
  5. Viewholder = new Viewholder (Convertview);
  6. LOG.I (TAG, "1:" + position);
  7. final int finalp = position;
  8. Recordmap.put (position, convertview);
  9. ViewHolder.checkBox.setOnClickListener (new Onclicklistener () {
  10. @Override
  11. public void OnClick (View v) {
  12. CheckBox checkbox = (checkbox) v;
  13. Ischecked.set (Finalp, checkbox.ischecked ());
  14. }
  15. });
  16. Convertview.settag (Viewholder);
  17. } Else {
  18. LOG.I (TAG, "2:" + position);
  19. Convertview = Recordmap.get (position);
  20. Viewholder = (Viewholder) convertview.gettag ();
  21. }
  22. ViewHolder.button.setText ("click" + position);
  23. ViewHolder.checkBox.setChecked (Ischecked.get (position));
  24. return convertview;
  25. }

-----------------------------------------------Program source code download

"Turn" Android:listview common dislocation of the checkbox dislocation

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.