直接上代码
ViewPager fvp;
List<ImageView> listvp;
// 创建小圆点
List<ImageView> listdoc;
// 小圆点布局
LinearLayout ll;
int count = 10000;
int[] imgarray = { R.drawable.b1, R.drawable.b2, R.drawable.b3,
R.drawable.b4 };
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
count++;
fvp.setCurrentItem(count);
sendEmptyMessageDelayed(0, 1000);
};
};
//onCreate方法中写入
fvp = (ViewPager) view.findViewById(R.id.fvp);
initData();
MyaAdapter adapter = new MyaAdapter();
fvp.setAdapter(adapter);
fvp.setCurrentItem(count);
handler.sendEmptyMessage(0);
// 添加小圆点的方法
addDoc();
fvp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
int index = arg0 % listvp.size();
for (int i = 0; i < imgarray.length; i++) {
if (index == i) {
listdoc.get(i).setImageResource(
R.drawable.point_selected);
} else {
listdoc.get(i).setImageResource(R.drawable.point_mormal);
}
}
}
@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
}
});
//调用的方法
private void addDoc() {
listdoc = new ArrayList<ImageView>();
ll = (LinearLayout) view.findViewById(R.id.ll);
for (int i = 0; i < imgarray.length; i++) {
ImageView iv = new ImageView(getActivity());
if (i == 0) {
iv.setImageResource(R.drawable.point_selected);
} else {
iv.setImageResource(R.drawable.point_mormal);
}
// 添加小圆点
listdoc.add(iv);
// 添加小圆点视图
ll.addView(iv);
}
}
private void initData() {
listvp = new ArrayList<ImageView>();
for (int i = 0; i < imgarray.length; i++) {
final int index = i;
ImageView iv = new ImageView(getActivity());
//点击图片跳转传值
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(getActivity(),
IntentActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
});
listvp.add(iv);
}
}
class MyaAdapter extends PagerAdapter {
@Override
public int getCount() {
// if(listvp != null){
// return listvp.size();
// }
return Integer.MAX_VALUE;
}
@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) {
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(listvp.get(position % listvp.size()));
return listvp.get(position % listvp.size());
}
}