Misplaced assignment of listview and GridView in Android

Listview and GridView call getview several times, resulting in misplaced assignment

Recently, when writing the GridView adapter assignment, when the last one or two need to define other pictures, the assignment error is caused by the adapter's getview calling many times, resulting in the data assignment error. The online search finally finds the solution and records it.

1、 Listview

1. Reasons

Because listview usually uses wrap_ Content, which is highly uncertain, causes the system to continuously measure, so the onmeasure method is called many times, so getview is called many times.

2. Solution

It's very simple. Just write the width and height to death (explicitly give a number or match_parent)

2、 GridView

1. Reasons

Getview will be called many times regardless of whether the height and width are dead or not. The reason remains to be explored,....

2. Solution

You cannot prevent multiple calls to getview. You can only call getview when onmeasure calls. Do not assign values to convertview. You can only operate convertview when onlayout.

1) . custom GridView:

2) Processing of convertview in. Getview

That's OK.

If you have any questions, please leave a message or go to the community of this site for communication and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>