<android.support.v4.view.ViewPager android:id="@+id/main_vp" android:layout_width="fill_parent" android:layout_height="fill_parent" />
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentBottom="true" android:gravity="center" android:layout_marginBottom="20dp" >
<View android:id="@+id/view1" android:layout_width="10dip" android:layout_height="10dip" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" />
<View android:id="@+id/view2" android:layout_width="10dip" android:layout_height="10dip" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="@drawable/view1" />
<View android:id="@+id/view3" android:layout_width="10dip" android:layout_height="10dip" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="@drawable/view1" />
</LinearLayout>
//选中前
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<corners android:radius="5dip" />
<solid android:color="#33ff00" />
</shape>
//选中后
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<corners android:radius="5dip" />
<solid android:color="#33ff00" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/ad" android:orientation="vertical" >
</LinearLayout>
public class MainActivity extends Activity {
private List<View> list;
private List<View> listDot;
private ViewPager vp;
private int oldposition = 0;
private int currentItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addLayout();
info();
}
private void addLayout() {
list = new ArrayList<View>();
View v1=View.inflate(this, R.layout.vp1, null);
View v2=View.inflate(this, R.layout.vp2, null);
View v3=View.inflate(this, R.layout.vp3, null);
v1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "点击了...", 0).show();
}
});
list.add(v1);
list.add(v2);
list.add(v3);
}
private void info() {
vp = (ViewPager) findViewById(R.id.main_vp);
View v1=findViewById(R.id.view1);
View v2=findViewById(R.id.view2);
View v3=findViewById(R.id.view3);
listDot = new ArrayList<View>();
listDot.add(v1);
listDot.add(v2);
listDot.add(v3);
vp.setAdapter(new MyAdapter());
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
listDot.get(oldposition).setBackgroundResource(R.drawable.view1);
listDot.get(arg0).setBackgroundResource(R.drawable.view2);
oldposition = arg0;
currentItem = arg0;
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
class MyAdapter extends PagerAdapter{
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(list.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(list.get(position));
return list.get(position);
}
}
@Override
protected void onStart() {
super.onStart();
ScheduledExecutorService s = Executors.newSingleThreadScheduledExecutor();
s.scheduleWithFixedDelay(new ViewPagerTask(), 2, 2, TimeUnit.SECONDS);
}
class ViewPagerTask implements Runnable{
@Override
public void run() {
currentItem = (currentItem+1) % list.size();
handler.obtainMessage().sendToTarget();
}
}
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
vp.setCurrentItem(currentItem);
}
};
protected void onStop() {
super.onStop();
};
}