Automatic switching of viewpager page with handler in Android App

There are automatic switching products on many e-commerce websites and apps. The promotion is fast. I feel that the experience is very good. I just learned to use viewpager today, so I also realized a demo with similar functions.

Here are two screenshots:

To implement a viewpager with automatic playback function, the main tasks are as follows:

Implement a viewpageradapter to provide display content for viewpager (for example, the above two kitten pictures)

Implement an onpagechangelistener, so that you can prompt the location of the current page after page switching (for example, in the above figure, the three dots in the lower left corner indicate the current page in red)

Implement a handler to modify the UI after a certain time interval (switch the currently displayed picture to the next)

The above three paragraphs are the main code. In addition, you also need to send a start message in onresume() and stop the automatic switching of viewpager pages in onstop(). The complete code is as follows:

The layout file is as follows: (the invokedactivity in the above code is very simple, which is omitted here)

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