图片轮播小圆点

-
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="200dp">





android:orientation="horizontal"
android:id="@+id/linear_layout"
android:layout_alignBottom="@+id/view_pager"

android:layout_gravity="center_horizontal"


android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">





-------------------------------------------------------------------------------------------------------------------------------------------------------
动态添加小圆点的方法
/**
* 动态添加小圆点
* @param list
*/
private void initDoc(List list) {
//1.需要一个集合记录一下小圆点的imageView控件
images = new ArrayList();
//2...linearLayout上面的视图清空一下再去添加
linearLayout.removeAllViews();


for (int i=0; iImageView imageView = new ImageView(MainActivity.this);


if (i==0){
imageView.setImageResource(R.drawable.shape_01);
}else {
imageView.setImageResource(R.drawable.shape_02);
}


//添加到集合去
images.add(imageView);
//添加到线性布局上


//这是布局参数,,刚开始小圆点之间没有距离,所以使用java代码指定宽度高度,并且指定小圆点之间的距离
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);


params.setMargins(5,0,5,0);


linearLayout.addView(imageView,params);
}


}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//3.根据集合的长度动态添加小圆点...linearLayout,集合是指获取的json的集合
initDoc(list);


//2.手动的可以无限滑动
viewPager.setCurrentItem(list.size()*100000); //设置当前展示中间某个足够大的位置


handler.sendEmptyMessageDelayed(0,2000); //发送一个延时的空消息


//viewPage设置监听事件
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {


}


/**
* 当选中某个页面的时候,把当前的小圆点背景变成绿色
* @param position
*/
@Override
public void onPageSelected(int position) {


for (int i=0; iif (i == position%images.size()){


images.get(i).setImageResource(R.drawable.shape_01);
}else {
images.get(i).setImageResource(R.drawable.shape_02);
}


}


}


@Override
public void onPageScrollStateChanged(int state) {


}
});
}


}
};


asyncTask.execute();

-------------------------------------------------------------------------------------------------------------------------------------
添加handler
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what == 0){
//viewPager显示下一页
viewPager.setCurrentItem(viewPager.getCurrentItem() +1);


//再次发送延时消息
handler.sendEmptyMessageDelayed(0,2000);
}
}
};

-----------------------------------------------------------------------------------------------------------------------------------------
适配器里需要配置
【图片轮播小圆点】public class MyAdapter extends PagerAdapter {
Context context;
List list;
Handler handler;


public MyAdapter(Context context, List list, Handler handler) {
this.context = context;
this.list = list;
this.handler = handler;
}


@Override
public int getCount() {
return Integer.MAX_VALUE;
}


@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}


/**
* viewPager具有预加载,默认的前后加载一页,,,默认的容器里面最多三页
* @param container
* @param position
* @return
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {


//1.把这个当前展示的视图添加到容器中...container
ImageView imageView = new ImageView(context);


//..........使图片平铺整个imageView控件
imageView.setScaleType(ImageView.ScaleType.FIT_XY);


//imageLoader加载图片到这个imageView控件上
ImageLoader.getInstance().displayImage(list.get(position %list.size()).getImg(),imageView,ImageLoaderUtil.getDefaultOption());


//给imageView设置触摸的监听事件
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {


int action = motionEvent.getAction(); //获取手指的动作
switch (action){
case MotionEvent.ACTION_DOWN://按下的动作...应该取消发送消息的操作
handler.removeCallbacksAndMessages(null);


break;
case MotionEvent.ACTION_MOVE://移动的动作
handler.removeCallbacksAndMessages(null);


break;
case MotionEvent.ACTION_CANCEL://取消
//重新发送
handler.sendEmptyMessageDelayed(0,2000);


break;
case MotionEvent.ACTION_UP://抬起的动作
handler.sendEmptyMessageDelayed(0,2000);


break;
}




//返回true表示自己处理触摸事件
return true;
}
});


container.addView(imageView); //添加到容器
//2.把当前展示的视图返回


return imageView;
}


@Override
public void destroyItem(ViewGroup container, int position, Object object) {


//销毁视图
container.removeView((View) object);
}
}

    推荐阅读