[AS2.3.3] for the infinite loop of viewpager, as2.3.3viewpager

Source: Internet
Author: User

[AS2.3.3] for the infinite loop of viewpager, as2.3.3viewpager

Recently, we want to create an infinite loop of viewpager. I checked it online!
Add an infinite loop on the homepage and the last page, and set the value of the infinite number of adapters to calculate the current position using the remainder!

Then I studied the addOnPageChangeListener event of viewpager.
Next let's talk about it.

First

There are three methods to implement this listener
OnPageScrolled (int position, float positionOffset, int positionOffsetPixels)
Monitor the sliding distance and the current page

OnPageSelected (int position)
After the listener slides successfully, it is displayed on the page.

OnPageScrollStateChanged (int state)
Monitor the slide Process

This is whereOnPageScrollStateChanged (int state)To make a simple judgment.
We need to know that three States exist.

 
ViewPager. SCROLL_STATE_IDLE
ViewPager. SCROLL_STATE_DRAGGING
ViewPager. SCROLL_STATE_SETTLING

Below I write the following code in the status judgment:

@ Override public void onPageScrollStateChanged (int state) {int cur = mViewPager. getCurrentItem (); int count = adapter. getCount (); switch (state) {case ViewPager. SCROLL_STATE_IDLE: if (cur = 0) {Log. e ("-s-", "first page regression idle");} else if (cur = count-1) {Log. e ("-s-", "Last page regression idle");} else {Log. e ("-s-", "other page regression idle");} break; case ViewPager. SCROLL_STATE_DRAGGING: if (cur = 0) {Log. e ("-s-", "dragging on the first page");} else if (cur = count-1) {Log. e ("-s-", "dragging the last page");} else {Log. e ("-s-", "dragging other pages");} break; case ViewPager. SCROLL_STATE_SETTLING: if (cur = 0) {Log. e ("-s-", "the first page is successfully placed");} else if (cur = count-1) {Log. e ("-s-", "Last page successfully placed");} else {Log. e ("-s-", "other pages are successfully placed") ;}break ;}}

Next, slide the viewpager with three pages as follows:
Slide the first page to the left (slide failure)
==> Slide the first page to the right (to the second page)
==> The second page slides to the right (to the last page)
==> Slide the last page to the right (unable to slide)
The log is as follows:

We can see that the first page is dragged and directly returned to idle because it cannot be swiped.

From the first page to the second page, the first page is displayed as a drag, and the second page is successfully placed, and the second page is idle.

Implement Infinite Loop

After the above log judgment, we can add a lock in the drag status, and then remove the lock in the place where it is placed. When a page is forcibly redirected when it is idle, a simple infinite loop can be realized.

We modified the code just now.

Private boolean isChange = false; @ Override public void onPageScrollStateChanged (int state) {int cur = mViewPager. getCurrentItem (); int count = adapter. getCount (); switch (state) {case ViewPager. SCROLL_STATE_IDLE: if (cur = 0) {if (! IsChange) {Log. e ("-s-", "not switched successfully, force jump to the last page"); mViewPager. setCurrentItem (count, false);} else {Log. e ("-s-", "first page regression idle") ;}} else if (cur = count-1) {if (! IsChange) {Log. e ("-s-", "not switched successfully, force jump to the first page"); mViewPager. setCurrentItem (0, false);} else {Log. e ("-s-", "Last page regression idle") ;}} else {Log. e ("-s-", "other page regression idle");} break; case ViewPager. SCROLL_STATE_DRAGGING: isChange = false; if (cur = 0) {Log. e ("-s-", "dragging on the first page");} else if (cur = count-1) {Log. e ("-s-", "dragging the last page");} else {Log. e ("-s-", "dragging other pages");} break; case ViewPager. SCROLL_STATE_SETTLING: isChange = true; Log. e ("-s-", "Switched successfully"); break ;}}

Then we are doing the operation just now.
Slide the first page to the left (force to the last page)
==> Slide the last page to the right (force to the first page)
==> Slide the first page to the right (to the second page)
==> The second page slides to the right (to the last page)

We have implemented loop operations and generated the following logs:

Summary

This achieves an infinite loop, but one thing is that, by directly dragging from the first page or the last page to a place that cannot be moved, the sliding distance Effect of viewpager will not appear. Therefore, we recommend that you add a new page on the homepage and the last page. Here we will only discuss the infinite loop.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.