Android achieves unlimited carousel and android achieves carousel

Source: Internet
Author: User

Android achieves unlimited carousel and android achieves carousel

1. Set the length of getCount () in the ViewPager adapter to an infinitely large Integer. MAX_VALUE

2 understand that the current currentIten is position % images. length;

3. Set the position of ViewPager at the beginning. viewPager. setCurrentItem (images. length) * 1000). In this way, you can slide to the left at the beginning.

4. Set the position of the small point in the OnPageChangeListener ViewPager sliding listener.

5. Set Handler to slide regularly to enable slide.

6. Send an empty message in onResume. Remove the message in onPause and clear all Callbacks and Messages to avoid Memory leakage.

The following figure shows that the main code image is local.

1 package com. demo. sb. main; 2 3 import java. util. arrayList; 4 import com. demo. sb. utils. densityUtil; 5 import com. demo. suibian. r; 6 import android. annotation. suppressLint; 7 import android. app. activity; 8 import android. OS. bundle; 9 import android. OS. handler; 10 import android. support. v4.view. pagerAdapter; 11 import android. support. v4.view. viewPager; 12 import android. support. v4.view. viewPager. onPageChangeListener; 13 import android. view. motionEvent; 14 import android. view. view; 15 import android. view. view. onClickListener; 16 import android. view. view. onTouchListener; 17 import android. view. viewGroup; 18 import android. widget. imageView; 19 import android. widget. linearLayout; 20 21 public class MActivity_Luenbo extends Activity {22 23 private ViewPager viewPager; 24 private LinearLayout group; 25 private int [] images = {R. drawable. a, R. drawable. c, R. drawable. d, 26 R. drawable. guide_1, R. drawable. guide_2}; 27 private ArrayList <ImageView> mImageViewList; 28 private ImageView [] diandian; // point 29 private int selectedItem on ViewPager = 0; 30 31 @ Override 32 protected void onCreate (Bundle savedInstanceState) {33 // TODO Auto-generated method stub 34 super. onCreate (savedInstanceState); 35 setContentView (R. layout. mactivity_luenbo); 36 viewPager = (ViewPager) findViewById (R. id. vp_viewPager); 37 group = (LinearLayout) findViewById (R. id. ll_viewGroup); 38 init (); 39 40} 41 42 @ SuppressLint ("HandlerLeak") 43 private Handler mHandler = new Handler () {44 public void handleMessage (android. OS. message msg) {45 viewPager. setCurrentItem (selectedItem + 1); 46 mHandler. sendEmptyMessageDelayed (0, 3000); 47}; 48}; 49 50 private void init () {51 // TODO Auto-generated method stub 52 // Bootstrap 3 ViewPager 53 mImageViewList = new ArrayList <ImageView> (); 54 for (int I = 0; I <images. length; I ++) {55 ImageView imageView = new ImageView (this); 56 imageView. setBackgroundResource (images [I]); 57 mImageViewList. add (imageView); 58} 59 60 // add the dot to the ViewGroup 61 diandian = new ImageView [images. length]; 62 for (int I = 0; I <images. length; I ++) {63 // Add the point image group 64 diandian [I] = new ImageView (this); 65 if (I = 0) {66 diandian [I]. setImageResource (R. drawable. page_indicator_focused); 67} else {68 diandian [I] 69. setImageResource (R. drawable. page_indicator_unfocused); 70} 71 diandian [I]. setPadding (8, 8, 8, 8); 72 group. addView (diandian [I]); 73} 74 viewPager. setAdapter (new GuideAdapter (); 75 // set the listener, mainly set the background of the dot 76 viewPager. setOnPageChangeListener (new GuidePagerListener (); 77 // set the default value of ViewPager to 1000 times the length. In this way, 78 viewPager slides to the left. setCurrentItem (images. length) * 1000); 79} 80 81/** 82 * each time when onResume has a focus, an empty message is sent to start carousel 83 */84 @ Override 85 protected void onResume () {86 // TODO Auto-generated method stub 87 super. onResume (); 88 mHandler. sendEmptyMessageDelayed (0, 2000); 89} 90 91/** 92 * Stop carousel 93 */94 @ Override 95 protected void onPause () {96 // TODO Auto-generated method stub 97 super. onPause (); 98 mHandler. removeCallbacksAndMessages (null); 99} 100 101/** 102 * ViewPager adapter 103*104 */105 private class GuideAdapter extends PagerAdapter {106 107 @ Override108 public int getCount () {109 // TODO Auto-generated method stub110 return Integer. MAX_VALUE; 111} 112 113 @ Override114 public boolean isViewFromObject (View arg0, Object arg1) {115 // TODO Auto-generated method stub116 return arg0 = arg1; 117} 118 119 // @ Override120 // public void destroyItem (View container, int position, Object object) 121 // {122 // TODO Auto-generated method stub123 // (ViewGroup) container ). removeView (images [position124 // % images. length]); 125 //} 126 @ Override127 public void destroyItem (ViewGroup container, int position, Object object) {128 // TODO Auto-generated method stub129 container. removeView (View) object); 130} 131 132/** 133 * load the image, dividing the current position by the length of the Image array to obtain the remainder is critical: 134 */135 @ Override136 public Object instantiateItem (ViewGroup container, int position) {137 // TODO Auto-generated method stub138 final int currentIten = position % images. length; 139 ImageView iView = mImageViewList. get (currentIten); 140 try {141 142 if (iView. getParent () = null) {143 iner. addView (iView); 144} 145 iView. setOnClickListener (new OnClickListener () {146 147 @ Override148 public void onClick (View arg0) {149 // TODO Auto-generated method stub150 DensityUtil. showToast (MActivity_Luenbo.this, 151 currentIten + ""); 152} 153}); 154 iView. setOnTouchListener (new NewTouchListener (); 155} catch (Exception e) {156 // TODO: handle exception157} 158 159 return iView; 160} 161 162} 163 164/** 165 * ViewPager sliding listener 166*167 * @ author Administrator168 * 169 */170 private class GuidePagerListener implements OnPageChangeListener {171 172 @ Override173 public void onPageScrollStateChanged (int arg0) {174 // TODO Auto-generated method stub175 176} 177 178 @ Override179 public void onPageScrolled (int arg0, float arg1, int arg2) {180 // TODO Auto-generated method stub181 System. out. println (arg0 + "dd" + arg1 + "dddd" + arg2); 182} 183 184 @ Override185 public void onPageSelected (int position) {186 // TODO Auto-generated method stub187 selectedItem = position; 188 System. out. println ("page number" + position + "" + position189 % mImageViewList. size (); 190 for (int I = 0; I <diandian. length; I ++) {191 if (I = position % images. length) {192 diandian [I] 193. setImageResource (R. drawable. page_indicator_focused); 194} else {195 diandian [I] 196. setImageResource (R. drawable. page_indicator_unfocused ); 197} 198} 199 200 201} 202 203/** 204 * ViewPager touch event when the caster stops carousel 205*206 * @ author Administrator207 * 208 */209 private class NewTouchListener implements OnTouchListener {210 211 @ Override212 public boolean onTouch (View arg0, motionEvent arg1) {213 // TODO Auto-generated method stub214 switch (arg1.getAction () {215 case MotionEvent. ACTION_DOWN: 216 mHandler. removeCallbacksAndMessages (null); 217 break; 218 case MotionEvent. ACTION_CANCEL: 219 mHandler. sendEmptyMessageDelayed (0, 3000); 220 break; 221 case MotionEvent. ACTION_UP: 222 mHandler. sendEmptyMessageDelayed (0, 3000); 223 break; 224 default: 225 break; 226} 227 return true; 228} 229 230} 231 232}
 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" > 6  7     <TextView 8         android:layout_width="match_parent" 9         android:layout_height="wrap_content"10         android:padding="10dp"11         android:text="@string/luenbo"12         android:textColor="#f00" />13 14     <FrameLayout15         android:layout_width="match_parent"16         android:layout_height="200dp" >17 18         <android.support.v4.view.ViewPager19             android:id="@+id/vp_viewPager"20             android:layout_width="match_parent"21             android:layout_height="match_parent" />22 23         <RelativeLayout24             android:layout_width="wrap_content"25             android:layout_height="wrap_content"26             android:layout_gravity="bottom|center"27             android:layout_marginBottom="20dp" >28 29             <LinearLayout30                 android:id="@+id/ll_viewGroup"31                 android:layout_width="wrap_content"32                 android:layout_height="wrap_content"33                 android:orientation="horizontal" />34         </RelativeLayout>35 36     </FrameLayout>37 38 </LinearLayout>

 

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.