Infinite Loop ViewPager, loop ViewPager

Source: Internet
Author: User

Infinite Loop ViewPager, loop ViewPager
Current situation

Without modifying the source code, when ViewPager slides to the last item, it cannot slide to the right. When ViewPager slides to the first item, he cannot move forward. (All the above are nonsense)

Ideas

We can think like this, when sliding the last one, we let him jump to the first one, so that he can continue to slide back, so that we want the cycle sliding.

In this case, although the function is a loop, it will automatically jump between the last one and the first one in actual display.

Optimization

We can add a fake item at the beginning and end of the original linked list, and use two additional items for the jump action, so as to avoid the error picture of automatic jump. See the following demo.

The following figure shows A, B, and C at 0, 1, and 2.

 

 

 

 

In fact, when we add data, we add two more. The last Interface C is added at location 0, and the first interface A is added at location 4.

 

When the interface slides to position 3, it can also slide to the right, which gives people the feeling of loop. However, when sliding to position 4, there is no more on the right side of him. Isn't that just a cover? Therefore, when sliding to position 4, jump to position 1 immediately. Because they are the same data, we can't see the jump from the display effect, so we actually changed to position 1, so we can continue to slide to the right.

Repeat the judgment of the above conditions to realize the cycle to the right.

Code Analysis

Perform condition judgment in onPageSelected and jump in onPageScrollStateChanged. The key code is as follows:

Initialize and add an item at the beginning and end.

// Add 1st interfaces. In fact, the last interface is addTextView (POINT_LENGTH-1 ); // Add the displayed 2, 3, and 4 interfaces for (int I = 0; I <3; I ++) {addTextView (I); addPoint (I );} // Add the last 5th interfaces. In fact, the first interface is addTextView (0 );

Condition determination:

@ Overridepublic void onPageSelected (int pPosition) {mIsChanged = true; if (pPosition> POINT_LENGTH) {mCurrentPagePosition = cursor;} else if (pPosition <FIRST_ITEM_INDEX) {mCurrentPagePosition = POINT_LENGTH ;} else {mCurrentPagePosition = pPosition;} Log. I (TAG, "the current position is" + mCurrentPagePosition); setCurrentDot (mCurrentPagePosition );}

Jump:

@Overridepublic void onPageScrollStateChanged(int pState) {if (ViewPager.SCROLL_STATE_IDLE == pState) {if (mIsChanged) {mIsChanged = false;mViewPager.setCurrentItem(mCurrentPagePosition, false);}}}

The complete logic is as follows:

Package com. ahacool. circleviewpager; import java. util. arrayList; import android. app. activity; import android. OS. bundle; import android. support. v4.view. pagerAdapter; import android. support. v4.view. viewPager; import android. support. v4.view. viewPager. onPageChangeListener; import android. util. log; import android. view. gravity; import android. view. view; import android. view. viewGroup; import android. widget. imageView; Import android. widget. LinearLayout. LayoutParams; import android. widget. TextView;/*** @ ClassName MainActivity * @ Description one way to move viewpager cyclically and smoothly. Implementation Method: add an interface at the beginning and end of the displayed interface. * @ Author Moto * @ date 2014 2014-7-18 **/public class MainActivity extends Activity implements OnPageChangeListener {private ViewPager mViewPager; private ViewGroup mPointViewGroup; private ArrayList <View> mViewPagerList; private boolean mIsChanged = false; private int mCurrentPagePosition = FIRST_ITEM_INDEX; private int mCurrentIndex; private static final int POINT_LENGTH = 3; private static final int FIRST_ITEM_INDEX = 1; private static final String TAG = "MOTO"; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initUI ();} private void initUI () {mViewPager = (ViewPager) findViewById (R. id. viewpager); mPointViewGroup = (ViewGroup) findViewById (R. id. point_layout); mViewPagerList = new ArrayList <View> (); // adds 1st interfaces. Display the last interface addTextView (POINT_LENGTH-1); // Add the actually displayed 2, 3, and 4 interfaces for (int I = 0; I <3; I ++) {addTextView (I); addPoint (I) ;}// add the last 5th interfaces. In fact, the first interface is addTextView (0 ); pagerAdapter pagerAdapter = new CustomPagerAdapter (mViewPagerList); mViewPager. setAdapter (pagerAdapter); mViewPager. setOnPageChangeListener (this); mViewPager. setCurrentItem (mCurrentPagePosition, false);} private void addTextView (int pIndex) {TextVi Ew textview = new TextView (this); textview. setLayoutParams (new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT); textview. setGravity (Gravity. CENTER); textview. setText ("this is the" + (pIndex + 1) + "page"); textview. setTextSize (50); mViewPagerList. add (textview);} private void addPoint (int pIndex) {ImageView pointImageView = new ImageView (this); LayoutParams layoutParams = new LayoutParams (20, 2 0); layoutParams. setMargins (10, 0, 10, 0); pointImageView. setLayoutParams (layoutParams); pointImageView. setBackgroundResource (R. drawable. point_style); if (0 = pIndex) {pointImageView. setEnabled (false);} mPointViewGroup. addView (pointImageView);} private void setCurrentDot (int positon) {// The serial number actually displayed on the page is 1st, 2, 3. The number of the vertex should be 0, 1, 2. so subtract 1. positon = positon-1; if (positon <0 | positon> mViewPagerList. size ()-1 | mCurrentIndex = positon) {return;} mPointViewGroup. getChildAt (positon ). setEnabled (false); mPointViewGroup. getChildAt (mCurrentIndex ). setEnabled (true); mCurrentIndex = positon;} @ Overridepublic void onPageScrollStateChanged (int pState) {if (ViewPager. SCROLL_STATE_IDLE = pState) {if (mIsChanged) {mIsChanged = false; mViewPager. setCurrentItem (mCurrentPagePosition, false) ;}}@ Overridepublic void evaluate (int arg0, float arg1, int arg2) {}@ Overridepublic void onPageSelected (int pPosition) {mIsChanged = true; if (pPosition> POINT_LENGTH) {mCurrentPagePosition = FIRST_ITEM_INDEX;} else if (pPosition <FIRST_ITEM_INDEX) {mCurrentPagePosition = POINT_LENGTH;} else {mCurrentPagePosition = pPosition;} Log. I (TAG, "the current position is" + mCurrentPagePosition); setCurrentDot (mCurrentPagePosition );}}

Source code in the address: https://github.com/bird7310/Demos.git

 

Summary

I hope to help you and give more comments. Recently, the project was very busy and I haven't read my blog for a long time. All the projects are numb, relax, and be lazy. write a blog.

 


What are the differences between the three infinite loops in Embedded C?

This is related to the compiler. If the compiler is optimized, the three results may be the same.

1. For (;); directly execute the loop body (empty statement) without judgment)
2. While (1); first judge, then execute the loop body (empty statement)
3. Do {;} while (1); first execute the loop body (empty statement) and then judge

09999 (an infinite loop of 9) must be smaller than 1?

Equal to 1.
Generally, the infinite understanding is infinite, that is, the process of being constructed. In your imagination, 0.9999... The subsequent 9 is still continuing, rather than being "reached" infinitely.
At present, the editors of formal teaching materials may not understand the differences between real infinity and Potential Infinity, or they only know that they are not differentiated out of understanding the principle of convenience, two Infinite concepts are often mixed. For example, for 0. 99999 ..... you should think of him as an infinitely finished one. There is no difference between him and 1. 0000 .... 1, because no matter how small it is, it is still a limited number. 0. 999 .. there should be an infinitely small d between and 1. d can be understood as a medium existence between 0 and a non-0 Number. it complies with the addition principle of 0, that is, x + d = x, but does not conform to the multiplication principle of 0, that is, x * d =x. if you cannot correctly understand the infinitely small nature, you will always feel that the integral Computation is just an approximate computation.

Related Article

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.