Android uses viewpager to realize left-right circular sliding and rotation effect

Viewpager is a commonly used Android component, but usually when we use viewpager, we can't realize left-right infinite loop sliding. When we slide to the boundary, we will see an animation that can't turn the page, which may affect the user experience. In addition, some regional viewpagers (such as display advertisements or announcements) may need the effect of automatic rotation, that is, users can see the information of other pages without sliding.

Implementation of circular sliding effect: pageradapter

We know that the sliding effect of viewpager is very excellent, so we basically don't need to deal with this sliding, only deal with the display of content. The display of content is controlled by the adapter, so the focus here is this adapter. For simplicity, each view in this example is directly a picture. The following is the code of the adapter:

Here are a few points to note:

Return value of getcount () method: this value is directly related to the "boundary" of viewpager, so when we set it to integer.max_ After value, the user can hardly see this boundary. Of course, it is usually possible to set it to 100 times the number of actual contents. This is what an implementation looked at earlier does.

Position processing of instantiateitem() method: because we set count to integer. Max_ Value, so the value range of this position is very large, but there must not be so much content to be displayed (there are often only a few items), so there must be modulo operation here. However, there will be problems with simple modulus calculation: if the user slides to the left, the position may have a negative value. So we need to process the negative value again to make it fall within the correct range.

Processing of the parent component of the instantiateitem () method: usually we will directly addview, but if we write this directly here, an IllegalStateException will be thrown. Assuming there are three views in total, this exception will be triggered when the user slides to the fourth because we try to add a view with a parent component to another component. However, if it is written directly as follows:

NullPointerException will be thrown because the component has no parent component at the beginning. Therefore, a judgment is needed. That is, the code above.

Destroyitem () method: since we have handled the remove logic in the instantiateitem () method, we don't need to handle it here. In fact, experiments show that if the remove call is added here, the content of viewpager will be empty.

Integration code: mainactivity

Simple layout activity_ main:

Code operation mainactivity:

Among them, the implementation of rotation effect: update with handler:

Here I define a handler to handle the rotation of viewpager. The so-called "rotation" effect is realized as follows: switch the displayed page every certain time (here is 3 seconds). By controlling each page to play circularly in a certain order, the effect of rotation is achieved. For this reason, we can use the sendemptymessagedelayed() method of the handler to realize the scheduled update, and note that the user may also manually slide the viewpager with the rotation effect. Therefore, I think the user wants to view the specified page at this time, and the rotation should be cancelled at this time.

The above is what Xiaobian introduced to you. Android uses viewpager to realize left-right circular sliding and rotation effect. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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