android教程viewpager自动循环和手动循环
package com.imoyo.expert;
import java.util.arraylist;
import android.app.activity;
import android.os.bundle;
import android.os.handler;
import android.support.v4.view.viewpager;
import android.support.v4.view.viewpager.onpagechangelistener;
import android.util.log;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.view.window;
import android.view.viewgroup.layoutparams;
import android.widget.imageview;
public class exactivity extends activity {
viewpager viewpager;
arraylist<view> list;
viewgroup main, group;
boolean cunhuan = false;
imageview imageview;
imageview[] imageviews;
private static int c_id = 0;
static int page_id = 1;
private runnable switchtask = new runnable() {
public void run() {
if (cunhuan) {
viewpager.setcurrentitem(page_id);
page_id++;
}
cunhuan=true;
log.e("is_run", cunhuan+"");
mhandler.postdelayed(switchtask, 3000);
}
};
handler mhandler = new handler();
@override
protected void onresume() {
super.onresume();
switchtask.run();
}
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
this.requestwindowfeature(window.feature_no_title);
getview();
imageviews = new imageview[list.size()];
viewgroup main = (viewgroup) inflater.inflate(r.layout.activity_main,
null);
viewgroup group = (viewgroup) main.findviewbyid(r.id.viewgroup);
viewpager = (viewpager) main.findviewbyid(r.id.viewpager);
for (int i = 0; i < 5; i++) {
imageview = new imageview(this);
imageview.setlayoutparams(new layoutparams(12, 12));
imageviews[i] = imageview;
if (i == 0) {
imageviews[i].setbackgroundresource(r.drawable.img_blue_point);
} else {
imageviews[i].setbackgroundresource(r.drawable.img_white_point);
}
group.addview(imageview);
}
setcontentview(main);
viewpageradapter adapter = new viewpageradapter(this, list);
viewpager.setadapter(adapter);
viewpager.setonpagechangelistener(listener);
viewpager.setcurrentitem(2);
}
public void getview() {
inflater = getlayoutinflater();
list = new arraylist<view>();
list.add(inflater.inflate(r.layout.item4, null));
list.add(inflater.inflate(r.layout.item5, null));
list.add(inflater.inflate(r.layout.item1, null));
list.add(inflater.inflate(r.layout.item2, null));
list.add(inflater.inflate(r.layout.item3, null));
list.add(inflater.inflate(r.layout.item4, null));
list.add(inflater.inflate(r.layout.item5, null));
list.add(inflater.inflate(r.layout.item1, null));
list.add(inflater.inflate(r.layout.item2, null));
}
onpagechangelistener listener = new onpagechangelistener() {
@override
public void onpageselected(int arg0) {
if (arg0 == 0) {
viewpager.setcurrentitem(5, false);
page_id=5;
} else if (arg0 == 8) {
viewpager.setcurrentitem(3, false);
page_id=3;
}else{
page_id=arg0;
}
log.e("arg", arg0 + "");
for (int i = 2; i < 7; i++) {
imageviews[i - 2]
.setbackgroundresource(r.drawable.img_white_point);
}
switch (arg0) {
case 7:
case 2:
imageviews[0]
.setbackgroundresource(r.drawable.img_blue_point);
break;
case 3:
case 8:
imageviews[1]
.setbackgroundresource(r.drawable.img_blue_point);
break;
case 4:
imageviews[2]
.setbackgroundresource(r.drawable.img_blue_point);
break;
case 0:
case 5:
imageviews[3]
.setbackgroundresource(r.drawable.img_blue_point);
break;
case 1:
case 6:
imageviews[4]
.setbackgroundresource(r.drawable.img_blue_point);
break;
}
}
@override
public void onpagescrolled(int arg0, float arg1, int arg2) {
}
@override
public void onpagescrollstatechanged(int arg0) {
}
};
private layoutinflater inflater;
}
下一篇: 图片分页查看 原创