关闭

vieapager小圆点

101人阅读 评论(0) 收藏 举报

import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;


public class MainActivity extends Activity {

private int[] imgs = {R.drawable.aaa,R.drawable.bbb,R.drawable.ccc,R.drawable.ddd};
private ViewPager vp;
private LinearLayout ll_dots;

private List<ImageView> imglist;
private ArrayList<ImageView> img_dots;
private Handler handler;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

vp = (ViewPager)findViewById(R.id.vp);
ll_dots = (LinearLayout)findViewById(R.id.ll_dots);



handler = new Handler(){

public void handleMessage(android.os.Message msg) {

int curr = vp.getCurrentItem();
Log.d("test", "test....."+curr);

//切换至下一个页面
vp.setCurrentItem(++curr);
handler.sendEmptyMessageDelayed(1, 2000);
};
};

// 初始化数据
initImages();


// 初始化小圆点
initDots();




vp.setAdapter(new MyPageAdapter(imglist,handler));

//初始化位置
vp.setCurrentItem(imglist.size()*100000);

//viewpager加监听
vp.setOnPageChangeListener(new OnPageChangeListener() {

@Override
public void onPageSelected(int position) {
for(int i=0;i<img_dots.size();i++){
if(position%img_dots.size() == i){
img_dots.get(i).setImageResource(R.drawable.lunbo_dot_focus);
}else{
img_dots.get(i).setImageResource(R.drawable.lunbo_dot_normal);
}
}

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {

}

@Override
public void onPageScrollStateChanged(int arg0) {

}
});





//实现无限轮播
handler.sendEmptyMessageDelayed(1, 2000);

}


private void initDots() {
img_dots = new ArrayList<ImageView>();
for(int i=0;i<imgs.length;i++){
ImageView iv = new ImageView(this);
if(i==0){
iv.setImageResource(R.drawable.lunbo_dot_focus);
}else{
iv.setImageResource(R.drawable.lunbo_dot_normal);
}
LayoutParams params = new LayoutParams(20, 20);
params.setMargins(5, 0, 5, 0);

//把小圆点放到页面的LinnearLayout容器中
ll_dots.addView(iv, params );
//把小圆点存放到集合中
img_dots.add(iv);
}
}


private void initImages() {
imglist = new ArrayList<ImageView>();
for(int i = 0;i<imgs.length;i++){
ImageView view = new ImageView(this);
view.setImageResource(imgs[i]);
imglist.add(view);
}

}




}



//监听

public Object instantiateItem(ViewGroup container, int position) {
ImageView img = imglist.get(position%imglist.size());
//点击图片的时候,不要无限轮播了
img.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_CANCEL://鼠标划走
handler.sendEmptyMessageDelayed(1, 2000);
break;


case MotionEvent.ACTION_DOWN://按下鼠标
handler.removeCallbacksAndMessages(null);//清空所有handler消息池的消息及所有回调函数
break;

case MotionEvent.ACTION_UP://鼠标抬起
handler.sendEmptyMessageDelayed(1, 2000);
break;
}


return true;
}
});

container.addView(img);

return img;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:834次
    • 积分:86
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章存档