Basic tutorial for Android -- 2.4.5 checkbox dislocation of ListView
Basic tutorial for Android -- 2.4.5 checkbox dislocation of ListView
Tags (separated by spaces): basic Android tutorial
This section introduces:
As one of the typical ListView problems, if you have tried to customize a ListView item with a checkbox on it, then
When the number of items exceeds one page, this problem will occur. Next we will analyze the causes of this problem and how
Solve this problem!
1. cause of the problem:
This is a picture of the ListView getView method calling mechanism found on the Internet.
There isRecyclerNormally, the items visible on ListView are in memory, and their items are placed in
In this Recycler, when the item is loaded for the first time, convertView on the current page is NULL. When getting out of the screen, this is the time.
ConvertViewIt is not empty, so the new one will reuse this ConvertView!
Let's write a simple example. Let's talk about the log. Below are some Log charts after running!
It can be seen that ConvertView is not empty since Postion is 12. What does it represent here,
I don't know. The source code is required for visual testing... We know that ConvertView will cache data here. This is why
So the first solution is not to reuse this ConvertView, or
This ConvertView is set to null for each getView operation. However, if the number of items to be displayed is large,
This method will appear very bloated. Generally, we use the following solution for actual development:
Find something to save the status of the current Item CheckBox. during initialization, check whether the Item is selected!
2. solution example:
There are many methods to store this Checkbox. You can put it in a HashMap.
public class Person implements Serializable{ private String name; private String number; private boolean checkStatus; public Person(String name, String number) { super(); this.name = name; this.number = number; this.checkStatus = false; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public boolean getCheckStatus() { return checkStatus; } public void setCheckStatus(boolean checkStatus) { this.checkStatus = checkStatus; }}
Implemented Adapter class:ContactListAdapter. java:
Public class ContactListAdapter extends BaseAdapter {private List
MData; private Context mContext; public ContactListAdapter (List
Data, Context context) {mData = data; mContext = context;} // defines a method for refreshing data public void changeData (List
Data) {mData = data; notifyDataSetChanged () ;}@ Override public int getCount () {return mData. size () ;}@ Override public Object getItem (int position) {return mData. get (position) ;}@ Override public long getItemId (int position) {return position ;}@ Override public View getView (int position, View convertView, ViewGroup parent) {final int index = position; ViewHolder viewHolder; if (convertView = null) {convertView = LayoutInflater. from (mContext ). inflate (R. layout. item_contact, parent, false); viewHolder = new ViewHolder (); viewHolder. ly = (RelativeLayout) convertView. findViewById (R. id. lyContactListItem); viewHolder.txt Name = (TextView) convertView .findViewById(R.id.txt Name); viewHolder.txt Number = (TextView) convertView .findViewById(R.id.txt Number); viewHolder. cbxStatus = (CheckBox) convertView. findViewById (R. id. cbxStatus); convertView. setTag (viewHolder);} else {viewHolder = (ViewHolder) convertView. getTag ();} viewHolder. cbxStatus. setOnCheckedChangeListener (new OnCheckedChangeListener () {@ Override public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {if (isChecked) mData. get (index ). setCheckStatus (true); else mData. get (index ). setCheckStatus (false) ;}}); if (mData. get (position ). getCheckStatus () = true) viewHolder. cbxStatus. setChecked (true); else viewHolder. cbxStatus. setChecked (false); viewHolder.txt Name. setText (mData. get (index ). getName (); viewHolder.txt Number. setText (mData. get (index ). getNumber (); return convertView;} private class ViewHolder {RelativeLayout ly; TextView txtName; TextView txtNumber; CheckBox cbxStatus ;}}
Hey, it's very simple. Also, don't forget:
The checkbox listener method must be added before the code in the Checkbox state is initialized ~