一个自动轮播以及小圆点的设置
自动图片轮播 先从网络获取图片,然后把图片放到适配器传过去,之后实行无线轮播
public class MainActivity extends AppCompatActivity {
private ViewPager vp;
List list=new ArrayList<>();
private ImageView[] imgArray;
Handler h=new Handler(){
@Override
public void handleMessage(Message msg) {
int index = vp.getCurrentItem();
vp.setCurrentItem(index+1);
setSelectedPoint(index);
h.sendEmptyMessageDelayed(1,2000);
}
};
private LinearLayout yuan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp = (ViewPager) findViewById(R.id.vp);
yuan = (LinearLayout)findViewById(R.id.yuan);
vp.setAdapter(new Myadapter(this,list));
//实现自动播放
h.sendEmptyMessageDelayed(1,2000);
xiaoyuandian();
}
publicvoid setSelectedPoint(int pageIndex){
for(int i=0;
i
imgArray[i].setImageResource(R.drawable.xuanzhong);
//小红点
} else {
imgArray[i].setImageResource(R.drawable.meiyouxuanzhong);
//小灰点
}
}
}
//设置小云点
private void xiaoyuandian() {
for(int i=0;
i
img.setScaleType(ImageView.ScaleType.FIT_XY);
if(i==0){
img.setImageResource(R.drawable.xuanzhong);
}else{
img.setImageResource(R.drawable.meiyouxuanzhong);
}
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(10,10);
params.setMargins(0,0,10,0);
yuan.addView(img,params);
imgArray[i]=img;
}
}
【一个自动轮播以及小圆点的设置】创建一个适配器继承pageradapeter
public class Myadapter extends PagerAdapter {
private Context ctx;
private int [] imgArray=new int[]{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d};
private List list;
public Myadapter(Context ctx, List list) {
this.ctx = ctx;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(Viewview, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView img=new ImageView(ctx);
img.setScaleType(ImageView.ScaleType.FIT_XY);
img.setImageResource(imgArray[position%imgArray.length]);
container.addView(img);
return img;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}