1.左右无限滑动
public class MainActivity extends AppCompatActivity {
private static ViewPager viewPager;
private RadioGroup group;
private int[] imageIds = {R.drawable.ym1, R.drawable.ym2, R.drawable.ym3, R.drawable.ym4};
private List<ImageView> mList;
private static int index = 0, preIndex = 0;
private boolean isContinue = true;
private Timer timer = new Timer();
private MyHandler mHandler;
public static class MyHandler extends Handler {
private WeakReference<MainActivity> weakReference;
public MyHandler(MainActivity activity) {
weakReference = new WeakReference<>(activity);
}
@Override
public void handleMessage(Message msg) {
if (weakReference.get() != null) {
index++;
viewPager.setCurrentItem(index);
}
super.handleMessage(msg);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
addListener();
viewPager.setCurrentItem(imageIds.length * 100);
initRadioButton(imageIds.length);
startSwitch();
}
/**
* 初始化控件
*/
public void initView() {
viewPager = (ViewPager) findViewById(R.id.viewpager);
group = (RadioGroup) findViewById(R.id.group);
}
/**
* 初始化数据
*/
public void initData() {
mList = new ArrayList<>();
viewPager.setAdapter(pagerAdapter);
mHandler = new MyHandler(this);
}
/**
* 添加监听
*/
public void addListener() {
viewPager.addOnPageChangeListener(onPageChangeListener);
viewPager.setOnTouchListener(onTouchListener);
}
/**
* 进行图片轮播
*/
public void startSwitch() {
timer.schedule(new TimerTask() {
@Override
public void run() {
if (isContinue) {
mHandler.sendEmptyMessage(1);
}
}
}, 3000, 3500);
}
/**
* 根据图片个数初始化按钮
* @param length 图片所在集合长度
*/
private void initRadioButton(int length) {
for (int i = 0; i < length; i++) {
ImageView imageview = new ImageView(this);
imageview.setImageResource(R.drawable.rg_selector);
imageview.setPadding(20, 0, 0, 0);
group.addView(imageview, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
group.getChildAt(0).setEnabled(false);
}
}
/**
* 根据当前触摸事件判断是否要轮播
*/
View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
isContinue = false;
break;
default:
isContinue = true;
}
return false;
}
};
/**
* 根据当前选中的页面设置按钮的选中
*/
ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
index = position;
setCurrentDot(index % imageIds.length);
}
@Override
public void onPageScrollStateChanged(int state) {
}
};
/**
* 设置对应位置按钮的状态
* @param i 当前位置
*/
private void setCurrentDot(int i) {
if (group.getChildAt(i) != null) {
group.getChildAt(i).setEnabled(false);
}
if (group.getChildAt(preIndex) != null) {
group.getChildAt(preIndex).setEnabled(true);
preIndex = i;
}
}
PagerAdapter pagerAdapter = new PagerAdapter() {
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
position = position % imageIds.length;
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(imageIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
container.addView(imageView);
mList.add(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
};
@Override
protected void onDestroy() {
super.onDestroy();
if (timer != null) {
preIndex = 0;
timer.cancel();
}
}
}
2.单向滑动
public class MainActivity extends AppCompatActivity {
private static ViewPager viewPager;
private RadioGroup group;
private int[] imageIds = {R.drawable.ym1, R.drawable.ym2, R.drawable.ym3, R.drawable.ym4};
private List<ImageView> mList;
private static int index = 0, preIndex = 0;
private boolean isContinue = true;
private Timer timer = new Timer();
private MyHandler mHandler;
public static class MyHandler extends Handler {
private WeakReference<MainActivity> weakReference;
public MyHandler(MainActivity activity) {
weakReference = new WeakReference<>(activity);
}
@Override
public void handleMessage(Message msg) {
if (weakReference.get() != null) {
index++;
viewPager.setCurrentItem(index);
}
super.handleMessage(msg);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
addListener();
initRadioButton(imageIds.length);
startSwitch();
}
/**
* 初始化控件
*/
public void initView() {
viewPager = (ViewPager) findViewById(R.id.viewpager);
group = (RadioGroup) findViewById(R.id.group);
}
/**
* 初始化数据
*/
public void initData() {
mList = new ArrayList<>();
viewPager.setAdapter(pagerAdapter);
mHandler = new MyHandler(this);
}
/**
* 添加监听
*/
public void addListener() {
viewPager.addOnPageChangeListener(onPageChangeListener);
viewPager.setOnTouchListener(onTouchListener);
}
/**
* 进行图片轮播
*/
public void startSwitch() {
timer.schedule(new TimerTask() {
@Override
public void run() {
if (isContinue) {
mHandler.sendEmptyMessage(1);
}
}
}, 3000, 3500);
}
/**
* 根据图片个数初始化按钮
* @param length 图片所在集合长度
*/
private void initRadioButton(int length) {
for (int i = 0; i < length; i++) {
ImageView imageview = new ImageView(this);
imageview.setImageResource(R.drawable.rg_selector);
imageview.setPadding(20, 0, 0, 0);
group.addView(imageview, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
group.getChildAt(0).setEnabled(false);
}
}
/**
* 根据当前触摸事件判断是否要轮播
*/
View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
isContinue = false;
break;
default:
isContinue = true;
}
return false;
}
};
/**
* 根据当前选中的页面设置按钮的选中
*/
ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
index = position;
setCurrentDot(index % imageIds.length);
}
@Override
public void onPageScrollStateChanged(int state) {
}
};
/**
* 设置对应位置按钮的状态
* @param i 当前位置
*/
private void setCurrentDot(int i) {
if (group.getChildAt(i) != null) {
group.getChildAt(i).setEnabled(false);
}
if (group.getChildAt(preIndex) != null) {
group.getChildAt(preIndex).setEnabled(true);
preIndex = i;
}
}
PagerAdapter pagerAdapter = new PagerAdapter() {
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
position = position % imageIds.length;
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(imageIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
container.addView(imageView);
mList.add(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mList.get(position));
}
};
@Override
protected void onDestroy() {
super.onDestroy();
if (timer != null) {
preIndex = 0;
index = 0;
timer.cancel();
}
}
}
3.加入一张图片的判断(最终版)
public class MainActivity extends AppCompatActivity {
private static ViewPager viewPager;
private RadioGroup group;
private int[] imageIds = {R.drawable.ym1};
private List<ImageView> mList;
private static int index = 0, preIndex = 0;
private boolean isContinue = true;
private Timer timer = new Timer();
private MyHandler mHandler;
public static class MyHandler extends Handler {
private WeakReference<MainActivity> weakReference;
public MyHandler(MainActivity activity) {
weakReference = new WeakReference<>(activity);
}
@Override
public void handleMessage(Message msg) {
if (weakReference.get() != null) {
index++;
viewPager.setCurrentItem(index);
}
super.handleMessage(msg);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
addListener();
initRadioButton(imageIds.length);
startSwitch();
}
/**
* 初始化控件
*/
public void initView() {
viewPager = (ViewPager) findViewById(R.id.viewpager);
group = (RadioGroup) findViewById(R.id.group);
}
/**
* 初始化数据
*/
public void initData() {
mList = new ArrayList<>();
viewPager.setAdapter(pagerAdapter);
mHandler = new MyHandler(this);
}
/**
* 添加监听
*/
public void addListener() {
viewPager.addOnPageChangeListener(onPageChangeListener);
viewPager.setOnTouchListener(onTouchListener);
}
/**
* 进行图片轮播
*/
public void startSwitch() {
timer.schedule(new TimerTask() {
@Override
public void run() {
if (isContinue) {
if(imageIds.length!=1)
mHandler.sendEmptyMessage(1);
}
}
}, 3000, 3500);
}
/**
* 根据图片个数初始化按钮
* @param length 图片所在集合长度
*/
private void initRadioButton(int length) {
for (int i = 0; i < length; i++) {
ImageView imageview = new ImageView(this);
if(length == 1){
imageview.setVisibility(View.GONE);
return;
}
imageview.setImageResource(R.drawable.rg_selector);
imageview.setPadding(20, 0, 0, 0);
group.addView(imageview, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
group.getChildAt(0).setEnabled(false);
}
}
/**
* 根据当前触摸事件判断是否要轮播
*/
View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
isContinue = false;
break;
default:
isContinue = true;
}
if(imageIds.length == 1){
return true;
}
return false;
}
};
/**
* 根据当前选中的页面设置按钮的选中
*/
ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
index = position;
setCurrentDot(index % imageIds.length);
}
@Override
public void onPageScrollStateChanged(int state) {
}
};
/**
* 设置对应位置按钮的状态
* @param i 当前位置
*/
private void setCurrentDot(int i) {
if (group.getChildAt(i) != null) {
group.getChildAt(i).setEnabled(false);
}
if (group.getChildAt(preIndex) != null) {
group.getChildAt(preIndex).setEnabled(true);
preIndex = i;
}
}
PagerAdapter pagerAdapter = new PagerAdapter() {
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
position = position % imageIds.length;
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(imageIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
container.addView(imageView);
mList.add(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mList.get(position));
}
};
@Override
protected void onDestroy() {
super.onDestroy();
if (timer != null) {
preIndex = 0;
index = 0;
timer.cancel();
}
}
}