1,私有化控件
private ViewPager vp;
private RadioGroup rg;
2,handler 里设置无限轮播
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
int currentItem = vp.getCurrentItem();
vp.setCurrentItem(currentItem += 1);
handler.sendEmptyMessageDelayed(0, 3000);
}
};
3主Activity里实现得到viepager的控件ID并设置适配器
设置viewpager的监听方法
private void initadapter() {
// 设置圆点
initdots();
// 设置监听
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
rg.check(arg0 % ad1.size());
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
vp.setCurrentItem(10000 / 2 - 10000 / 2
% ad1.size());
handler.sendEmptyMessageDelayed(0, 3000);
};
根据viewpager的数量设置要添加的圆点radioGroup
private void initdots() {
// TODO Auto-generated method stub
int wrap = RadioGroup.LayoutParams.WRAP_CONTENT;
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(wrap, wrap);
params.leftMargin = 5;
for (int i = 0; i < ad1.size(); i++) {
RadioButton rb = new RadioButton(getActivity());
rb.setId(i);
rb.setButtonDrawable(R.drawable.x_dot_selector);
if (i != 0) {
rg.addView(rb, params);
} else {
rg.addView(rb);
}
rg.check(0);
}
}
//viewPager的适配器
Context context;
List<Ad1> ad1;
private BitmapUtils map;
public MyHomePagerAdapter(Context context, List<Ad1> ad1) {
// TODO Auto-generated constructor stub
this.context = context;
this.ad1 = ad1;
map = new BitmapUtils(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 10000;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
//得到网络图片地址
Ad1 ad=ad1.get(position%ad1.size());
String path=ad.getImage();
//得到imageView对象
ImageView iv=new ImageView(context);
LayoutParams params=new LayoutParams();
params.width=LayoutParams.MATCH_PARENT;
params.height=LayoutParams.MATCH_PARENT;
iv.setLayoutParams(params);
iv.setScaleType(ScaleType.FIT_XY);
map.display(iv, path);
container.addView(iv);
return iv;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
container.removeView((View) object);
}