In Android development, ListView, RecyclerView and GridView controls are indispensable tools for implementing list layout, I think it’s safe to say. For example, QQ friend list, we can not be implemented in a circular way, which has a great impact on the performance of the application. So, in order to record this problem before, and also incidentally for other children who need to solve this problem, I wrote this article (great god do not spray)!

Of course, if your layout is simple and you say I don’t need to reuse it, then you can see here to return….

Android ListView reuse optimization

  • Set the CheckBox of thesetOnClickListener()Listening to the
  • Set the CheckBox of thesetOnCheckedChangeListener()Listening Two solutions are proposed for these two implementations:
1. BysetOnClickListener()Monitor implementation

In addition to the text information and image information, I add a hasChecked attribute to indicate whether the item is selected. The entity class information is as follows:

private String description; Private int resId; Id private Boolean hasChecked; // Whether it is selectedCopy the code

Then we implement the listener in the Adapter of the Listview:

mCheckBox.setOnClickListener(new View.OnClickListener() {@override public void onClick(View v) {// check whether the control is checkedif(McHeckbox.ischecked ()){// If mdata.get (position).sethaschecked ()true); SelectCount++; }else {
                    mData.get(position).setHasChecked(true); selectCount--; }}});Copy the code

Now, you’re probably saying, well, that’s what I wrote, so why not mine? The reason for the confusion is that the Checkbox of each item is not initialized, so the layout control reuse will use the value of the reused control. So, we simply assign an initial value to the item’s checkbox before setting the setOnClickListener() listener.

mCheckBox.setChecked(mData.get(position).isHasChecked());
Copy the code
2. BysetOnCheckedChangeListener()Monitor implementation

One thing in common between this method and method 1 is that you need to assign an initial value to item, but the difference is that if you debug your code you can see that when you scroll, it will automatically call onCheckChanged() without clicking on the checkbox, Then go to execute the logic inside, the problem is still not solved!

At that time, we only need before setting the initial value for the checkbox to cancel setOnCheckedChangeListener () to monitor can solve this problem!

mCheckBox.setOnCheckedChangeListener(null);
mCheckBox.setChecked(mData.get(position).isHasChecked());
mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    mData.get(position).setHasChecked(true);
                    selectCount++;
                }else {
                    mData.get(position).setHasChecked(false); selectCount--; }}});Copy the code

Then you can see the effect like this:

Ok, so that’s the end of the two methods. Do you think it’s easy to cook chicken? If it helps you, please click “like”. Thank you.

CSDN Blog Portal – click on the CSDN blog homepage to see the sea by snail
Click on me to download the demo source oh