Perfectly solve the related problems about prohibiting viewpager preloading

Recently, I encountered another small problem at work, that is, the problem of viewpager preloading. I believe most people who have used viewpager have encountered this situation, and there are only a few online solutions. Finally, under my own continuous experiments, I perfectly solved (prohibited) the preloading of viewpager.

Well, first of all, let's explain what is the preloading of viewpager: viewpager has a "preloading" mechanism. By default, it will pre initialize the left and right adjacent pages of the current position of viewpager (commonly known as preloading). Its default value is 1. The advantage of this is that the left and right sliding of viewpager will be more smooth.

However, my situation is very special because one of my five fragments has a surfaceview. The problem is that when I slide the viewpager to its adjacent page, the page containing the surfaceview will be initialized in advance, and then the surfaceview will start previewing, but we can't see it. Similarly, when we slide from a page containing a surfaceview to its adjacent page, the surfaceview does not call back its surfacestore method. So it caused me great trouble.

OK, let's get down to business. How can we disable the preloading problem of viewpager?

Scheme 1: most online statements are lazy loading, that is, let the viewpager preload the initialization UI, and delay the loading of specific data and network access requests. This is because there is a setuservisiblehint (Boolean isvisibletouser) method in the fragment. We can judge in this method and load data only when its true is visible (that is, switch to a specific fragment), so as to save traffic. But this does not meet my needs, because a fragment will not be destroyed when the viewpager slides to its adjacent fragment. This can only solve some people's problems.

First, let's learn more about the Preloading Mechanism of viewpager:

As mentioned above, the default number of viewpager preloads is 1, which we can see in the viewpager source code.

DEFAULT_ OFFSCREEN_ Pages here defines that the default value is 1, so there is an online solution that calls the setoffsetscreenpagelimit (int limit) of viewpager to set the number of viewpager preloads. However, it is clear that this solution is not feasible, as shown in the viewpager source code below:

We can see that if you call this method and the value passed in is less than 1, it is invalid and will be forcibly dragged back to 1. And default_ OFFSCREEN_ The value of pages is private, and the subclass inheriting viewpager is also invisible.

Scheme 2: there is a second saying on the Internet. Customize a viewpager, copy all the native viewpagers, and modify the default_ OFFSCREEN_ The pages value is 0. Yes, that's the solution!! But!!

But!!! But what? But I tried. It didn't work. Why? The next is the focus of this paper.

Because now Android is 6.0, and the versions are old and high. In fact, although Android has V4 packages for each version, these V4 packages are different. This will cause the viewpager in the higher version V4 package to default even if you copy it_ OFFSCREEN_ Changing the value of pages to 0 still doesn't work, and the logic changes. I didn't continue to study where the specific changes led to invalidity. After all, the company won't take time for me to study these. Snicker

Perfect solution: OK, so the perfect solution to prohibit the preloading of viewpager is to use the viewpager in the lower version V4 package, copy it completely, and delete the default_ OFFSCREEN_ Change the pages value to 0. Bloggers personally test API 14, that is, viewpager is valid in the V4 package of Android 4.0.

Of course, since Google has such a viewpager mechanism, it must have its reason, so it is generally better to preload.

Finally, because fewer and fewer people will download the lower version of the source code, here you can directly post the viewpager that prohibits preloading. Take it if you need it. Copy will work.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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