Android solves the memory overflow problem of viewpager loading a large number of pictures

1. We all know that building an adapter for viewpager inherits pageradapter, so we won't talk about how to build it. Viewpager loads the current page and the left and right pages of the current page by default. At the beginning, the current page is subscript 0, so page 0 (subscript, the same below) and page 1 are loaded by default at the beginning. When you slide to the right and the current page is page 1, viewpager will load page 2. At this time, a total of 3 pages exist (pages 0, 1 and 2). Slide to the right again. When the current page is page 2, page 0 will be removed and page 3 will be loaded. Similarly, when the current page is page 1, page 3 will be removed. So you should understand.

Knowing the above principle, you can make the viewpager always load only 3 pages of pictures. Loading is to execute the instantiateitem (...) method, and removing is to execute the destroitem (...) method. You can load the pictures of a page in instantiateitem (...) and recycle the pictures of a page in destroitem (...). Attach Code:

2. Code example:

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
分享
二维码
< <上一篇
下一篇>>