Detailed explanation of Android viewpager circular advertising example

Detailed explanation of an example of viewpager circular playing advertisement bar for Android

We often see that the banner interface of some apps can play multiple advertising pictures in a loop and slide the loop manually. Seeing that effect, I believe everyone will think of viewpager, but viewpager does not support circular page turning, so you need to do it yourself to realize the cycle. Finally, another problem is how to smoothly transition from page turning to the last page to the home page. These are all due to a private letter asking me how to smooth the transition of the viewpager banner. For this reason, I want to implement and share it myself. This article is written to solve these problems.

1. Initialize layout

Let's write a layout of the viewpager banner first:

The effect at this time is as follows. Does it already look a little like:

2. Activity implementation

Next, we go to the key implementation code. Of course, the implementation of activity is not difficult for us. Among them, viewpager data is directly simulated with local data for convenience. In the actual project development, most of the data come from the network.

As mentioned above, we have solved the problem of smooth transition. The key is the viewpageadapter:

The getcount method returns integer.max_ VALUE

Initialize the content to be displayed for each entry. We take the position position% collection. Size

At this time, we found that the last advertisement can make a smooth transition, but there is another problem: how can we slide to the left from the first one? In fact, it is also easy to solve, that is, set the first entry to integer. Max_ The middle position of value is OK. Let's go to the initview method and set the number of entries to be displayed by the current viewpager: @ H_ 502_ 48@

The problem is solved perfectly.

The shape related resources used are as follows:

selector_ dot.xml

dot_ enable.xml

dot_ normal.xml

Let's take a look at the renderings:

So far, a smooth transition viewpager banner has been perfectly realized.

Thank you for reading, hope to 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
分享
二维码
< <上一篇
下一篇>>