布局代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.viewpager.widget.ViewPager
android:id="@+id/test_view_pager"
android:layout_width="match_parent"
android:layout_height="250dp"/>
<LinearLayout
<!--指示器小圆点-->
android:id="@+id/test_point_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/test_view_pager"
android:gravity="center_horizontal"></LinearLayout>
</RelativeLayout>
ViewPagerTestActivity
package com.wdf.zmkfsdk;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;import android.widget.ImageView;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
public class ViewPagerTestActivity extends AppCompatActivity {
ViewPager pager;
LinearLayout layout;
int[] imgId = {R.mipmap.pic_1,R.mipmap.pic_2,R.mipmap.pic_3};
List<ImageView> vpList;
List<ImageView> pointList;
private static Handler handler = new Handler(Looper.getMainLooper());
public static class MyRunnable implements Runnable{
@Override
public void run() {
int item = pager.getCurrentItem();
if(item == vpList.size()-1){
pager.setCurrentItem(0);
}else {
item++;
pager.setCurrentItem(item);
}
handler.postDelayed(new MyRunnable(),5000);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager_test);
pager = findViewById(R.id.test_view_pager);
layout = findViewById(R.id.test_point_ll);
initPager();
setPagerListener();
handler.postDelayed(new MyRunnable(),5000);
}
private void initPager() {
vpList = new ArrayList<>();
pointList = new ArrayList<>();
for(int i = 0; i < imgId.length;i++){
ImageView imageView = new ImageView(this);
imageView.setImageResource(imgId[i]);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(params);
vpList.add(imageView);
ImageView pIv = new ImageView(this);
pIv.setImageResource(R.mipmap.point_nomal);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(20, 20);
p.setMargins(5,0,5,0);
pIv.setLayoutParams(p);
pointList.add(pIv);
layout.addView(pIv);
}
pointList.get(0).setImageResource(R.mipmap.point_focus);
MyPagerAdapter adapter = new MyPagerAdapter(this,vpList);
pager.setAdapter(adapter);
}
private void setPagerListener(){
pager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
for(int i=0;i<pointList.size();i++){
pointList.get(i).setImageResource(R.mipmap.point_nomal);
}
pointList.get(position).setImageResource(R.mipmap.point_focus);
}
});
}
}
MyPagerAdapter
package com.wdf.zmkfsdk;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import java.util.List;
public class MyPagerAdapter extends PagerAdapter {
Context context;
List<ImageView> ivList;
public MyPagerAdapter(Context context, List<ImageView> ivList) {
this.context = context;
this.ivList = ivList;
}
@Override
public int getCount() {
return ivList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView imageView = ivList.get(position);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
ImageView imageView = ivList.get(position);
container.removeView(imageView);
}
}
效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bafea45c2a8b3b1c03e165f006407c91.png)
补充
Handler的使用
LayoutParams的详解
Handler警告
Handler警告处理