20 ViewPager Demo4自动轮播
MainActivity.java
思想:才用非常大的数 让其看起来可以循环轮播图片并且用户可以从尽头滑到首图的特点 、
package com.qf.day20_viewpager_demo4;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ViewPager viewPager;
private int[] imags = {R.drawable.image1,R.drawable.image2,R.drawable.image3};
private List<ImageView> list;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
handler.sendEmptyMessageDelayed(1, 2000);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewPager);
//数据源
list = new ArrayList<ImageView>();
if(imags.length<4){
for(int i=0;i<imags.length*4;i++){
ImageView imageView = new ImageView(getApplicationContext());
imageView.setImageResource(imags[i%imags.length]);
list.add(imageView);
}
}else{
for(int i=0;i<imags.length;i++){
ImageView imageView = new ImageView(getApplicationContext());
imageView.setImageResource(imags[i]);
list.add(imageView);
}
}
//adapter
viewPager.setAdapter(new MyAdapter());
//让程序默认展示第一条数据
viewPager.setCurrentItem(Integer.MAX_VALUE/2-Integer.MAX_VALUE/2%list.size());
//使用Handler发送消息
handler.sendEmptyMessageDelayed(1, 2000);
//自动轮播
// new Thread(){
// public void run() {
// while(true){
// try {
// Thread.sleep(2000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// runOnUiThread(new Runnable() {
//
// @Override
// public void run() {
// // 方式一
//// int pos = viewPager.getCurrentItem();
//// if(pos == imags.length-1){
//// viewPager.setCurrentItem(0,false);
//// }else{
//// viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
//// }
//
//
// viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
// }
// });
// }
// };
// }.start();
}
public class MyAdapter extends PagerAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;//设置一个最大值
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
container.addView(list.get(position%list.size()));
return list.get(position%list.size());
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
// super.destroyItem(container, position, object);
container.removeView(list.get(position%list.size()));
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
}
}
布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>