此代码实现:
首先无限轮播的实现,
当按住轮播图时,无限轮播停止 ,可以滑动图片, 小圆点跟着改变
当停止滑动时,有实现自动轮播
一.设置小圆点
在drawable 目录下 创建shape 文件:
创建两个文件:
point_selected.xml
point_un_selected.xml
二.布局
.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent">.support.v4.view.ViewPager>
【Android|无限自动轮播+自定义小圆点】三.main方法:
private ViewPager vp;
private ImageView imageView;
//存放图片地址的集合
List list = new ArrayList<>();
//存放小圆点的集合
List imageViews;
private LinearLayout linearLayout;
private Handlerhandler=new Handler(){
@Override
public void handleMessage(Message msg) {
//获取当前正在显示的页面
int index = vp.getCurrentItem();
//设置显示的页面
vp.setCurrentItem(index+1);
//改变小圆点
setSelectedPoint(index%list.size());
//延迟发送消息
sendEmptyMessageDelayed(1,2000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
vp = (ViewPager) findViewById(R.id.vp);
linearLayout = (LinearLayout) findViewById(R.id.lin);
//向集合中添加地址
list.add("http://img4.imgtn.bdimg.com/it/u=691759172,804438098&fm=214&gp=0.jpg");
list.add("http://k.zol-img.com.cn/sjbbs/7099/a7098662_s.jpg");
list.add("http://f2.topitme.com/2/44/48/11321074391a748442o.jpg");
//初始化小圆点圆点
initdata();
//设置适配器
vp.setAdapter(new Myadapter());
//设置当前显示页面(可以左右滑动)
vp.setCurrentItem(list.size()*100);
//实现自动播放
handler.sendEmptyMessageDelayed(1,2000);
//切换页面监听
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//设置小圆点
setSelectedPoint(position%list.size());
}@Override
public void onPageSelected(int position) {}@Override
public void onPageScrollStateChanged(int state) {}
});
//触摸监听
vp.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {int action=event.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
Log.d("zzz","--按下--");
//清空handler消息
handler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_UP:
Log.d("zzz","--抬起--");
//当停止触摸时,重新发消息 无限轮播
handler.sendEmptyMessageDelayed(1,2000);
break;
case MotionEvent.ACTION_MOVE:
Log.d("zzz","--移动--");
break;
case MotionEvent.ACTION_CANCEL:
Log.d("zzz","--取消--");
break;
}
return false;
}
});
} //适配器
classMyadapterextends 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) {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//从网络上获取图片 ,需要到入一个Myapp的工具类
ImageLoader.getInstance().displayImage(list.get(position%list.size()),imageView,MyApp.getDefaultDisplayOption());
container.addView(imageView);
return imageView;
}@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// super.destroyItem(container, position, object);
container.removeView((View) object);
}
}//设置小圆点的方法
public voidsetSelectedPoint(int index)
{for (inti=0;
i();
for(int i=0;
i