你的位置:首页 > 操作系统

[操作系统]安卓实现无限轮播


首先是布局界面

<?  android:layout_width="match_parent"  android:layout_height="match_parent" >  <android.support.v4.view.ViewPager    android:id="@+id/viewpager"    android:layout_width="match_parent"    android:layout_height="wrap_content" />  <LinearLayout    android:id="@+id/ll"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_alignParentBottom="true"    android:layout_centerHorizontal="true"    android:layout_marginBottom="60dp"    android:orientation="horizontal" >    <ImageView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_gravity="center_vertical"      android:clickable="true"      android:padding="15.0dip"      android:scaleType="fitCenter"      android:src="@drawable/point" />    <ImageView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_gravity="center_vertical"      android:clickable="true"      android:padding="15.0dip"      android:src="@drawable/point" />    <ImageView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_gravity="center_vertical"      android:clickable="true"      android:padding="15.0dip"      android:src="@drawable/point" />    <ImageView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_gravity="center_vertical"      android:clickable="true"      android:padding="15.0dip"      android:src="@drawable/point" />  </LinearLayout></RelativeLayout>

在设置android:src="@drawable/point" 的布局

 

<?selector ="http://schemas.android.com/apk/res/android">  <item android:state_enabled="true" android:drawable="@drawable/point_normal" />  <item android:state_enabled="false" android:drawable="@drawable/point_select" /></selector>

View Code

 

 接下来是主界面代码

 1 public class MianActivity extends Activity implements OnPageChangeListener,OnTouchListener { 2   private LinearLayout linearLayout; 3   // 定义ViewPager对象 4   private ViewPager viewPager; 5   // 定义ViewPager的适配器 6   private MyPageAdapter vpAdapter; 7   // 定义一个ArrayList来存放View 8   private ArrayList<View> views; 9   // 底部小点的图片 10   private ImageView[] points; 11   // 定义资源文件 12   private static final int images[] = { R.drawable.a, R.drawable.b, 13       R.drawable.c, R.drawable.d }; 14   // 记录当前选中位置 15   private int currentIndex; 16  17   // 18   private boolean isTaskRun=false; 19   private Timer mTimer; 20  21   @Override 22   protected void onCreate(Bundle savedInstanceState) { 23     // TODO Auto-generated method stub 24     super.onCreate(savedInstanceState); 25     setContentView(R.layout.main); 26     initView(); 27     initData(); 28     // startTask(); 29     // new Thread(new Runnable() { 30     // 31     // @Override 32     // public void run() { 33     // 34     // } 35     // }); 36  37   } 38  39   @SuppressLint("HandlerLeak") 40   private Handler mHandler = new Handler() { 41     @Override 42     public void handleMessage(Message msg) { 43       viewPager.setCurrentItem(msg.what); 44     } 45   }; 46   /** 47    * 停止计时任务 48   */ 49   private void stopTask(){ 50     if (isTaskRun) { 51       isTaskRun = false; 52       mTimer.cancel(); 53       mTimer = null; 54  55     } 56   } 57   /** 58    * 开启定时任务 59   */ 60   private void startTask() { 61     if (!isTaskRun) { 62       isTaskRun = true; 63       mTimer = new Timer(); 64  65       mTimer.schedule(new TimerTask() { 66  67         @Override 68         public void run() { 69           int position = viewPager.getCurrentItem(); 70           mHandler.sendEmptyMessage(++position); 71  72         } 73       }, 2000, 4000);// 这里设置自动切换的时间 74     } 75   } 76  77   private void initView() { 78     linearLayout = (LinearLayout) super.findViewById(R.id.ll); 79     viewPager = (ViewPager) super.findViewById(R.id.viewpager); 80     views = new ArrayList<View>(); 81     // 实例化ViewPager适配器 82     vpAdapter = new MyPageAdapter(views); 83      84   } 85  86   private void initData() { 87     // 定义一个布局并设置参数 88     // LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams( 89     // LinearLayout.LayoutParams.FILL_PARENT, 90     // LinearLayout.LayoutParams.FILL_PARENT); 91     LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams( 92         LinearLayout.LayoutParams.MATCH_PARENT, 93         LinearLayout.LayoutParams.MATCH_PARENT); 94  95     // 初始化引导图片列表 96     for (int i = 0; i < images.length; i++) { 97       ImageView iv = new ImageView(this); 98       iv.setLayoutParams(mParams); 99       iv.setScaleType(ScaleType.FIT_XY);// 设置缩放样式100       iv.setImageResource(images[i]);101       views.add(iv);102     }103     // 实例化ViewPager适配器104     vpAdapter = new MyPageAdapter(views);105     // 设置数据106     viewPager.setAdapter(vpAdapter);107     viewPager.setOnTouchListener(this);108     // 设置监听109 110     // 初始化底部小点111     points = new ImageView[images.length];112 113     // 循环取得小点图片114     for (int i = 0; i < images.length; i++) {115       // 得到一个LinearLayout下面的每一个子元素116       points[i] = (ImageView) linearLayout.getChildAt(i);117       // 默认都设为灰色118       points[i].setEnabled(true);119       // 给每个小点设置监听120       // points[i].setOnClickListener(this);121       // 设置位置tag,方便取出与当前位置对应122       points[i].setTag(i);123     }124 125     // 设置为白色,即选中状态126     points[currentIndex].setEnabled(false);127     startTask();128     viewPager.setOnPageChangeListener(this);129   }130 131   // 当滑动状态改变时调用132   @Override133   public void onPageScrollStateChanged(int arg0) {134     // TODO Auto-generated method stub135     // state:0代表无动作;1代表正在滑动;2代表滑动完毕136     boolean isAutoPlay = false;137 138     switch (arg0) {139     case 1:140       isAutoPlay = false;141       if (viewPager.getCurrentItem() == 0) {142 143         new Handler().postDelayed(new Runnable() {144           @Override145           public void run() {146             viewPager.setCurrentItem(1);147           }148         }, 4000);149       }150 151       break;152     case 2:153       isAutoPlay = true;154       break;155     case 0:156       if (viewPager.getCurrentItem() == viewPager.getAdapter().getCount() - 1157           && !isAutoPlay) {158 159         new Handler().postDelayed(new Runnable() {160           @Override161           public void run() {162             viewPager.setCurrentItem(0);163           }164         }, 5000);165       }166 167       if (viewPager.getCurrentItem() == 0) {168 169         new Handler().postDelayed(new Runnable() {170           @Override171           public void run() {172             //173             viewPager.setCurrentItem(1);174           }175         }, 5000);176       }177 178       break;179     }180   }181 182   // 当当前页面被滑动时调用183   @Override184   public void onPageScrolled(int arg0, float arg1, int arg2) {185     // TODO Auto-generated method stub186 187   }188 189   // 当新的页面被选中时调用190   @Override191   public void onPageSelected(int positon) {192     for (int i = 0; i < points.length; i++) {193       points[positon].setImageResource(R.drawable.point_select);194       if (positon != i) {195         points[i].setImageResource(R.drawable.point_normal);196       }197 198     }199   }200   @Override201   public boolean onTouch(View arg0, MotionEvent event) {202     if (event.getAction() == MotionEvent.ACTION_DOWN) {203       stopTask();204     }205     if (event.getAction() == MotionEvent.ACTION_UP) {206       startTask();207 208     }209     return false;210   }211 212 213 }

View Code

定义ViewPager适配器

 1 public class MyPageAdapter extends PagerAdapter { 2   // 界面列表 3   private ArrayList<View> views; 4  5   public MyPageAdapter(ArrayList<View> views) { 6     this.views = views; 7   } 8  9   @Override10   public int getCount() {11 //    if (views != null) {12 //      return Integer.MAX_VALUE;13 //    }14 //    return 0;15     return views.size();16   }17 18   /**19    * 初始化position位置的界面20   */21   @Override22   public Object instantiateItem(ViewGroup container, int position) {23 //    //24 //    container.addView(views.get(position % views.size()));25 //    return views.get(position % views.size());26     try {27       container.addView((View) views.get(position % views.size()),28           0);29     } catch (Exception e) {30     }31 32     return views.get(position % views.size());33   }34 35   /**36    * 判断是否由对象生成界面37   */38   @Override39   public boolean isViewFromObject(View view, Object arg1) {40     return (view == arg1);// 这行代码很重要,它用于判断你当前要显示的页面41   }42 43   /**44    * 销毁position位置的界面45   */46   @Override47   public void destroyItem(ViewGroup container, int position, Object arg2) {48     container.removeView(views.get(position % views.size()));49   }50 51 }