ViewPager加载网络图片+自动轮播+小圆点指示

时间:2022-01-19 20:43:06

handler接收消息更新pager页

Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 0){viewpager.setCurrentItem(viewpager.getCurrentItem() + 1);
pagercirculation();
}
}
};

创建一个ImageView类型的集合,用imageloader将图片加载并添加到集合

//这里我们先创建一个图片网络地址的集合
List<String> imgurl_list = new ArrayList<String>();
imgurl_list.add("图片地址1");
imgurl_list.add("图片地址2");
imgurl_list.add("图片地址3");
imgurl_list.add("图片地址4");
imgurl_list.add("图片地址5");



//加载网络图片保存到集合
List<ImageView> listimg = new ArrayList<ImageView>();
for (int i = 0; i < imgurl_list.size(); i++) {
ImageView imageView = new ImageView(this);
//设置加载下的图片宽度
imageView.setScaleType(ScaleType.FIT_XY);
loader.displayImage(imgurl_list.get(i), imageView);
listimg.add(imageView);
}
//设置适配器
viewpager.setAdapter(new MyPagerAdapter(listimg));
//设置小圆点
setdot(imgurl_list);

// 利用handler改变viewpager页面
public void pagercirculation() {
handler.sendEmptyMessageDelayed(0, 3000);
}

ViewPage适配器

public class MyPagerAdapter extends PagerAdapter {
private List<ImageView> listimg;

public HomePagerAdapter() {
super();
}

public MyPagerAdapter(List<ImageView> listimg) {
super();
this.listimg = listimg;
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}

@Override
public int getCount() {
return Integer.MAX_VALUE;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = listimg.get(position % listimg.size());
container.addView(view);
return view;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}

}

ViewPager添加底部小圆点

// 给viewpager设置小圆点
private void setdot(final List<String> imgurl_list ) {
int wrop = RadioGroup.LayoutParams.WRAP_CONTENT;
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(wrop, wrop);
params.leftMargin = 5;
for (int i = 0; i < list.size(); i++) {
RadioButton button = new RadioButton(getActivity());
button.setEnabled(false);
button.setId(i);
button.setButtonDrawable(R.drawable.dot_selector);
if (i == 0) {
pager_radiogroup.addView(button);
} else {
pager_radiogroup.addView(button, params);
}
}
pager_radiogroup.check(0);
viewpager.setOnPageChangeListener(new OnPageChangeListener() {

@Override
public void onPageSelected(int arg0) {
pager_radiogroup.check(arg0 % list.size());
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {

}

@Override
public void onPageScrollStateChanged(int arg0) {

}
});
}

XML布局中的代码

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >


<ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="150.0dip"
android:focusable="true" />


<RadioGroup
android:id="@+id/pager_radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/viewpager"
android:layout_alignParentEnd="true"
android:orientation="horizontal" />

</RelativeLayout>

ViewPager下小圆点的状态选择器

dot_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/dot_focus" android:state_checked="true"/>
<item android:drawable="@drawable/dot_normal" />

</selector>

dot_focus.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >

<solid android:color="#fd6f3e"/>
<size android:width="10dip"
android:height="10dip"/>


</shape>



dot_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >

<solid android:color="#b7abab"/>
<size android:width="10dip" android:height="10dip" />

</shape>