Android uses viewpager to achieve infinite sliding effect

preface

In fact, the principle is quite simple when you think about it carefully. Nothing more than when we slide to the last page and then slide back to the first page; When we slide to the first page and then forward, we navigate to the last page.

However, I believe many friends have encountered this problem: the excessive effect of view is unnatural.

Xiaobian also found many solutions through Baidu and Google, experimented with many methods, summarized a relatively good method, and then shared the sliding effect, implementation details and some stepped pits.

1. Infinite sliding effect (left and right infinite sliding)

Prepare 2 slide pictures in advance (if you have a little partner who wants to experiment, bring your own pictures, Xiaobian won't provide them...)

In order to display more intuitively, Xiaobian only used 2 pictures.

2. Code implementation

(1) XML layout file of activity (very simple layout)

(2)Activity

(3)ViewPagerAdapter

Where specific explanations are needed, corresponding comments are made in the code, so there will be no more explanations, but the following Xiaobian will also explain the two pits encountered.

3. Detail stepping pit

I believe that before looking at the implementation of Xiaobian, someone has found many similar implementations on the Internet. What Xiaobian wants to talk about next is stepping on the pit of "these similar implementations".

1. Exception: java.lang.illegalstateexception: the specified child already has a parent. You must call removeview() on the child's parent first

Reason: there are less than 4 picture resources and less than 4 corresponding imageviews.

As we all know, viewpager generally maintains 2 ~ 3 pages. If there are only 3 imageviews, this is likely to happen. After generating 3 pages and preparing to generate the fourth page, the system will report an exception and give you a prompt: you must call removeview() on the child's parent first (this means that you can remove the sub view of the front page from the front page first, and then add the sub view to page 4).

Solution: when there is only one picture, it cannot slide and will not be processed temporarily; If there are 2 ~ 3 images, the recursion is increased to 4 or more imageviews (the code to solve this problem is shown below).

2. The sliding effect is confused (the chaotic effect picture will be attached later)

Reason: there is a problem with the viewpager source code itself - the data range is out of bounds.

The Xiaobian's method for obtaining the number of viewpager pages is as follows:

When it is less than 1, the page does not slide and the number of pages does not need to be expanded;

When it is greater than 1, take the maximum value of 32767 in the short value range (we will have 3W multiple pages, but they will not exist at the same time, because the viewpager can maintain up to 3 pages)

Xiaobian uses short here, but at the beginning, similar implementations found on the Internet all use integer (integer. Max_value = 2147483647, that is, our viewpager will have about 2.1 billion pages). If integer is used, careful friends will find that sliding is often chaotic.

Page reverse rebound bug

When we let go, the page rebounds in reverse (1 ~ 2 pages), which is inconsistent with our expected sliding effect.

This is one of the bugs, and there are some slide bugs that are not easy to explain. Xiaobian explained it with the term "slide chaos"

Solution: reduce the number of pages (the small editor uses short instead of integer to reduce the number of pages).

For the specific critical value without bug, interested partners can experiment. The test results of Xiaobian are as follows:

Within 8 million: basically no sliding bug;

About 10 million: the bug with inaccurate positioning of the current page begins to appear, but there is no reverse rebound bug yet.

summary

The above is all about Android using viewpager to achieve infinite sliding effect. I hope this article can be helpful to everyone when developing. If you have any questions, you can leave a message.

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