废话不多说,直接上代码**************************
指示器的工具类
public class PageIndicator implements ViewPager.OnPageChangeListener {
private int mPageCount;//页数
private List<ImageView> mImgList;//保存img总个数
private int img_select;
private int img_unSelect;
public PageIndicator(Context context, LinearLayout linearLayout, int pageCount) {
this.mPageCount = pageCount;
mImgList = new ArrayList<>();
img_select = R.drawable.dot_select;
img_unSelect = R.drawable.dot_unselect;
final int imgSize = 25;
for (int i = 0; i < mPageCount; i++) {
ImageView imageView = new ImageView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//为小圆点左右添加间距
params.leftMargin = 10;
params.rightMargin = 10;
//给小圆点一个默认大小
params.height = imgSize;
params.width = imgSize;
if (i == 0) {
imageView.setBackgroundResource(img_select);
} else {
imageView.setBackgroundResource(img_unSelect);
}
//为LinearLayout添加ImageView
linearLayout.addView(imageView, params);
mImgList.add(imageView);
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
@Override
public void onPageSelected(int position) {
for (int i = 0; i < mPageCount; i++) {
//选中的页面改变小圆点为选中状态,反之为未选中
if ((position % mPageCount) == i) {
(mImgList.get(i)).setBackgroundResource(img_select);
} else {
(mImgList.get(i)).setBackgroundResource(img_unSelect);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {}
}
小圆点选中 dot_select
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<corners android:radius="1000dp" />
</shape>
小圆点未选中
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#323232" />
<corners android:radius="1000dp" />
</shape>
主要实现方法:
public class MainActivity extends AppCompatActivity {
private ViewPager vp_loop_advertisement;
private LinearLayout dot_horizontal;
private ArrayList<ImageView> imgList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int[] img = new int[]{R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.c};//图片自己找
imgList = new ArrayList<>();
for (int i = 0; i < img.length; i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(img[i]);
imgList.add(imageView);
}
dot_horizontal = (LinearLayout) findViewById(R.id.dot_horizontal);
vp_loop_advertisement = (ViewPager) findViewById(R.id.vp_loop_advertisement);
vp_loop_advertisement.setAdapter(new MyAdapter());
vp_loop_advertisement.addOnPageChangeListener(new PageIndicator(this, dot_horizontal, img.length));
}
private class MyAdapter extends PagerAdapter {
@Override
public int getCount() {
return imgList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView imageView = imgList.get(position % imgList.size());
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}
}
对应的xml文件activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.view.ViewPager
android:id="@+id/vp_loop_advertisement"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/dot_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="32dp"
android:gravity="center"
android:orientation="horizontal" />
</FrameLayout>
</android.support.constraint.ConstraintLayout>