Android listview reuse view causes confusion in selection, androidlistview

Source: Internet
Author: User

Android listview reuse view causes confusion in selection, androidlistview

20150526

Listview is a commonly used control. Custom adapters are often used. To improve the display efficiency, we often use the view reuse method to prevent re-painting. However, because the reuse uses the old view, usually, the position of the displayed data is disordered. I have encountered this problem many times in an app project, including buttons. However, today, when the item in listview has togglelbutton, the bound listener is CompoundButton of togglebutton. onCheckedChangeListener () has encountered a problem and has not been resolved. Finally, the item listener is changed to View. onClickListener () to solve the problem.

Generally, in order to prevent data confusion, if-else of null will be judged by convertview before obtaining the display data in the list. An example of getview is as follows:

1 @ Override 2 public View getView (int position, View convertView, ViewGroup parent) 3 {4 // TODO Auto-generated method stub 5 final ViewHolder holder; 6 // optimize listview -- remove reuse to prevent errors in the togglebutton click position record. 7 if (convertView = null) 8 {9 // use the custom Layout 10 holder = new ViewHolder (); 11 convertView = mInflater 12. inflate (R. layout. list_invite_party_member, null); 13 // initialize the element 14 holder in the layout. ivAvatar = (ImageView) ConvertView. findViewById (R. id. iv_avater); 15 holder. tvName = (TextView) convertView. findViewById (R. id. TV _name); 16 holder. tvTag = (TextView) convertView. findViewById (R. id. TV _tag); 17 holder. btnSelect = (ToggleButton) convertView 18. findViewById (R. id. btn_select); 19 holder. linearLayout = (LinearLayout) convertView 20. findViewById (R. id. rl_friend_item); 21 convertView. setTag (holder); 22} else 23 {24 holder = (ViewHolder) convertView. getTag (); 25} 26 27 // bind data 28 final int index = position; 29 UserBean bean = listFriend. get (index); 30 // set the Avatar 31 if (! TextUtils. isEmpty (bean. getUserAvatar () 32 {33 String avatarUrl = Constant. URL_USER_AVATER + bean. getUserAvatar (); 34 // initialization asynchronously loads the Avatar object 35 finalBitmap = FinalBitmap. create (context); 36 finalBitmap. configLoadingImage (R. drawable. user_head_02); 37 finalBitmap. display (holder. ivAvatar, avatarUrl); 38} else 39 {40 holder. ivAvatar. setImageResource (R. drawable. user_head_02); 41} 42 43 if (! TextUtils. isEmpty (bean. getUserNickname () 44 {45 holder. tvName. setText (bean. getUserNickname (); 46} else 47 {48 holder. tvName. setText (bean. getUserPhone (); 49} 50 if (! TextUtils. isEmpty (CommonUtils. getUserTags (bean) 51 {52 holder. tvTag. setText (CommonUtils. getUserTags (bean); 53 holder. tvTag. setVisibility (View. VISIBLE); 54} else 55 {56 holder. tvTag. setVisibility (View. GONE); 57} 58 if (TextUtils. equals (bean. getReserved01 (), "1") 59 {60 // Delete 61 holder when added. btnSelect. setChecked (true); 62} else 63 {64 holder. btnSelect. setChecked (false); 65} 66 holder. BtnSelect. setOnClickListener (new View. onClickListener () {67 68 @ Override 69 public void onClick (View v) {70 // TODO Auto-generated method stub 71 ToggleButton view = (ToggleButton) v; 72 // boolean isCheckedOld = view. getText (). toString (). equals ("add ")? True: false; 73 // check status 74 boolean isChecked = view after obtaining the latest click. isChecked (); 75 if (isChecked) 76 {77 // This person is added, and the button displays the deletion of 78 views. setChecked (true); 79 listFriend. get (index ). setReserved01 ("1"); 80 81} else 82 {83 // It was originally selected. After clicking it, the person was deleted 84 // The person was deleted, the button shows adding 85 views. setChecked (false); 86 listFriend. get (index ). setReserved01 ("0"); 87} 88} 89}); 90/* holder. btnSelect 91. setOnCheckedChangeListener (new CompoundButton. onCheckedChangeListener () 92 {93 94 @ Override 95 public void onCheckedChanged (CompoundButton buttonView, 96 boolean isChecked) 97 {98 99 LogUtil. d ("isChecked =" + isChecked); 100 LogUtil. d ("index =" + index); 101 // TODO Auto-generated method stub102 if (isChecked) 103 {104 // added this person, the button displays the deletion of 105 listFriend. get (index ). setReserved01 ("1"); 106} else107 {108 // This person is deleted, and the button displays the addition of 109 listFriend. get (index ). setReserved01 ("0"); 110} 111} 112}); */113 return convertView; 114} 115 116/** 117 * layout element 118 */119 class ViewHolder120 {121 ImageView ivAvatar; 122 TextView tvName; 123 TextView tvTag; 124 ToggleButton btnSelect; 125 LinearLayout linearLayout; 126}View Code


The final index is used to remember the data location. The correct data can be obtained in the action of the button listener below.

However, the experiment found that the use of this method is still not feasible. It may be a problem with CompoundButton. OnCheckedChangeListener. Using this listener to listen to the button action to change the corresponding list data will cause location confusion. You have to use View. OnClickListener () to control the display of togglebutton. Togglebutton is generally the two control methods. Note that the check status of the togglebutton will change as long as it is clicked, and the check status will change in View. OnClickListener.

Mark, so it is very easy to use the adapter to remember the status of btn, that is, the failure caused by failing to understand the CompoundButton mechanism. Fortunately, there is a View. the success of OnClickListener () is a precedent. Otherwise, it would be unrealistic to draw a large amount of list data for each view. There is another item in the checkbox that has not been tried. If not, you should replace it with a button or View. OnClickListener.

Related Article

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.