效果图
xml布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:layout_marginTop="50dp"
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="80dp"/>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_marginStart="244dp"
android:layout_below="@+id/viewpager"
android:id="@+id/point"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal">
</LinearLayout>
</RelativeLayout>
.class处理
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
LinearLayout linearLayout;
List<ImageView> imageViewList=new ArrayList<ImageView>();
int currentItem=300;//当前item位置
Timer mTimer;
int[] resId={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.b};
Handler myHandler=new Handler();
TimerTask timerTask=new TimerTask() {
@Override
public void run() {
myHandler.post(runnable);
}
};
private void start() {
if (mTimer==null){
mTimer=new Timer();
}
mTimer.schedule(timerTask,3000,3000);
}
/**
* 更新UI操作
*/
Runnable runnable=new Runnable() {
@Override
public void run() {
currentItem++;
int index=currentItem%4;
updatePoint(index);//初始化小圆点
viewPager.setCurrentItem(currentItem);
}
};
/**
* 初始化小圆点(如果不需要小圆点则不加)
*/
private void initPoint(int i) {
ImageView point=new ImageView(this);
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(20,20);
point.setLayoutParams(params);
if (i==0){
point.setImageResource(R.drawable.point_checked);
}else {
point.setImageResource(R.drawable.point_normal);
}
linearLayout.addView(point);
}
/**
* 更新小圆点(如果不需要小圆点则不加)
* @param index
*/
private void updatePoint(int index) {
for (int i=0;i<imageViewList.size();i++){
ImageView image= (ImageView) linearLayout.getChildAt(i);
if (i==index){
image.setImageResource(R.drawable.point_checked);
}else {
image.setImageResource(R.drawable.point_normal);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout=(LinearLayout)findViewById(R.id.point);
viewPager=(ViewPager)findViewById(R.id.viewpager);
for (int i=0;i<resId.length;i++){
ImageView imageView=new ImageView(this);
imageView.setImageResource(resId[i]);
imageViewList.add(imageView);
initPoint(i); //圆点
}
MyAdapter adapter=new MyAdapter();
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(currentItem); //一定要在setAdapter后面
start();
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
/**
* 更新当前item数目(currentPosition)转换item时调用此方法
* @param position
*/
@Override
public void onPageSelected(int position) {
currentItem=position;
int index=position%imageViewList.size();
updatePoint(index);//更新小圆点
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override
protected void onStop() {
super.onStop();
if (mTimer!=null) {
mTimer.cancel();
mTimer = null;
}
}
public class MyAdapter extends PagerAdapter {
public MyAdapter() {
super();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
int index = position % imageViewList.size(); //循环imageViewList里面的内容
((ViewPager) container).addView(imageViewList.get(index));
return imageViewList.get(index);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView(imageViewList.get(position % imageViewList.size()));
}
/**
* 设定tab数目为Integer.MAX_VALUE
*
* @return
*/
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
myHandler.removeCallbacks(runnable);
}
}
以上即可实现
可以参考:https://blog.csdn.net/yancychas/article/details/77892269