这里主要写广告轮播,布局文件就是一个简单viewpager
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 100) {
mviewPager.setCurrentItem(currentItem);
}
}
};
private class SlideShowTask implements Runnable {
@Override
public void run() {
synchronized (mviewPager) {
currentItem = (currentItem + 1) % list.size();
handler.sendEmptyMessage(100);
}
}
}
private void startPlay() {
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.scheduleAtFixedRate(new SlideShowTask(), 1, 3,
TimeUnit.SECONDS);
}
//在onCreate方法:
if (isAutoPlay) {
startPlay();
}
initViews();
//然后创建initViews()方法
dotViewList = new ArrayList<ImageView>();
list = new ArrayList<ImageView>();
for (int i = 0; i < 3; i++) {
ImageView dotView = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
params.leftMargin = 10;
params.rightMargin = 10;
params.height = 20;
params.width = 20;
if (i == 0) {
dotView.setBackgroundResource(R.drawable.banner_dian_focus);
} else {
dotView.setBackgroundResource(R.drawable.banner_dian_blur);
}
dotLayout.addView(dotView, params);
dotViewList.add(dotView);
}
ImageView img1 = (ImageView) inflater.inflate(R.layout.scroll_vew_item,
null);
ImageView img2 = (ImageView) inflater.inflate(R.layout.scroll_vew_item,
null);
ImageView img3 = (ImageView) inflater.inflate(R.layout.scroll_vew_item,
null);
img1.setBackgroundResource(R.drawable.image_title1);
img2.setBackgroundResource(R.drawable.image_title2);
img3.setBackgroundResource(R.drawable.image_title1);
list.add(img1);
list.add(img2);
list.add(img3);
img1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
img2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
img3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
ImagePaperAdapter adapter = new ImagePaperAdapter(
(ArrayList<ImageView>) list);
mviewPager.setAdapter(adapter);
mviewPager.setCurrentItem(0);
mviewPager.setOnPageChangeListener(new MyPageChangeListener());
//创建滑动监听implements OnPageChangeListener
boolean isAutoPlay = false;
@Override
public void onPageScrollStateChanged(int arg0) {
switch (arg0) {
case 1:
isAutoPlay = false;
break;
case 2:
isAutoPlay = true;
break;
case 0:
if (mviewPager.getCurrentItem() == mviewPager.getAdapter()
.getCount() - 1 && !isAutoPlay) {
mviewPager.setCurrentItem(0);
} else if (mviewPager.getCurrentItem() == 0 && !isAutoPlay) {
mviewPager.setCurrentItem(mviewPager.getAdapter()
.getCount() - 1);
}
break;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int pos) {
currentItem = pos;
for (int i = 0; i < dotViewList.size(); i++) {
if (i == pos) {
((View) dotViewList.get(pos))
.setBackgroundResource(R.drawable.banner_dian_focus);
} else {
((View) dotViewList.get(i))
.setBackgroundResource(R.drawable.banner_dian_blur);
}
}
}
点击查看更多内容
13人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦