public class MainActivity extends BaseActivity {
@BindView(R.id.viewPager)
ViewPager viewPager;
@BindView(R.id.llDots)
LinearLayout llDots; //小圆点容器
private int imags[] = {R.mipmap.banner1, R.mipmap.banner2, R.mipmap.banner3};
List<ImageView> imgList = new ArrayList<>(); //imageView集合
private List<View> dots = new ArrayList<>(); //小圆点集合
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
}
};
@Override
protected int getLayoutId() {
return R.layout.activity_main;
}
@Override
protected void initView(ViewGroup viewGroup) {
initViewPager(); //绑定viewPager
setOnPageChangeListener(); //绑定viewPager小圆点
}
private void initViewPager() {
for (int i = 0; i < imags.length; i++) {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setScaleType(ImageView.ScaleType.FIT_XY); //设置ImageView占据容器大小
Glide.with(MainActivity.this).load(imags[i]).into(imageView);
imgList.add(imageView);
//加载小圆点
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dot_item, null);
View viewById = view.findViewById(R.id.viewDots);
dots.add(viewById);
llDots.addView(view);
}
viewPager.setAdapter(new PagerAdapter(){
@Override
public int getCount() {
//使用Integer的最大值
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return object == view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
//注意这里不要使用
//container.removeView(list.get(position));
}
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
//当前位置模上总数等于图片在数组中的位置
position %= imgList.size();
if (position < 0) {
position = imgList.size() + position;
}
ImageView imageView = imgList.get(position);
ViewGroup parent = (ViewGroup) imageView.getParent();
if (parent != null) {
parent.removeView(imageView);
}
container.addView(imageView);
return imgList.get(position);
}
});
viewPager.setCurrentItem(Integer.MAX_VALUE / 2); //设置ViewPager从中间开始播放
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
SystemClock.sleep(3000);
handler.sendEmptyMessage(0);
}
}
}).start();
}
private void setOnPageChangeListener() {
final int DOTS_ITEM_NORMAL_WIDTH = 4; //小圆点未选中宽度
final int DOTS_ITEM_FOCUS_WIDTH = 16; //小圆点选中宽度
dots.get(0).setBackgroundResource(R.drawable.dots_focus);
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) dots.get(0).getLayoutParams();
linearParams.width = ConvertUtils.dp2px(DOTS_ITEM_FOCUS_WIDTH);
dots.get(0).setLayoutParams(linearParams);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
}
@Override
public void onPageScrollStateChanged(int i) {
if (i == 2) {
for (int j = 0; j < imgList.size(); j++) {
dots.get(j).setBackgroundResource(R.drawable.dots_normal);
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) dots.get(j).getLayoutParams();
linearParams.width = ConvertUtils.dp2px(DOTS_ITEM_NORMAL_WIDTH);
dots.get(j).setLayoutParams(linearParams);
}
dots.get(viewPager.getCurrentItem()%imgList.size()).setBackgroundResource(R.drawable.dots_focus);
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) dots.get(viewPager.getCurrentItem()%imgList.size()).getLayoutParams();
linearParams.width = ConvertUtils.dp2px(DOTS_ITEM_FOCUS_WIDTH);
dots.get(viewPager.getCurrentItem()%imgList.size()).setLayoutParams(linearParams);
}
}
});
}
}
MainActivity.class