此案例是实现页面的自滚动,且中间有小圆圈随着连动:
private void setIndicator() {
layout=(LinearLayout) findViewById(R.id.pagerIndicator);
for (int i = 0; i < images.length; i++) {
View view=new View(this);
LinearLayout.LayoutParams params=
new LinearLayout.LayoutParams(10,10);
view.setLayoutParams(params);
view.setBackgroundResource(R.drawable.indicator_selector_01);
layout.addView(view);
}
layout.getChildAt(0).setEnabled(false);
}
layout=(LinearLayout) findViewById(R.id.pagerIndicator);
for (int i = 0; i < images.length; i++) {
View view=new View(this);
LinearLayout.LayoutParams params=
new LinearLayout.LayoutParams(10,10);
view.setLayoutParams(params);
view.setBackgroundResource(R.drawable.indicator_selector_01);
layout.addView(view);
}
layout.getChildAt(0).setEnabled(false);
}
private int currentPager;
private void setViewPager() {
viewPager=(ViewPager) findViewById(R.id.guidePagerId);
PagerAdapter adapter=new GuideAdapter();
viewPager.setAdapter(adapter);
currentPager=0;
viewPager.setCurrentItem(currentPager);
//让其自动滚动
final Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
currentPager++;
if (currentPager==10000) {
currentPager=0;
}
viewPager.setCurrentItem(currentPager);
handler.postDelayed(this, 3000);
}
}, 3000);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) {
}
private void setViewPager() {
viewPager=(ViewPager) findViewById(R.id.guidePagerId);
PagerAdapter adapter=new GuideAdapter();
viewPager.setAdapter(adapter);
currentPager=0;
viewPager.setCurrentItem(currentPager);
//让其自动滚动
final Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
currentPager++;
if (currentPager==10000) {
currentPager=0;
}
viewPager.setCurrentItem(currentPager);
handler.postDelayed(this, 3000);
}
}, 3000);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
private int prePos;
@Override
public void onPageSelected(int position) {
layout.getChildAt(position%6).setEnabled(false);//show blue
layout.getChildAt(prePos).setEnabled(true);//show hui
prePos=position%6;
}
});
}
class GuideAdapter extends PagerAdapter{
@Override
public int getCount() {
return 10000;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView iv=new ImageView(MainActivity.this);
iv.setScaleType(ScaleType.FIT_XY);
iv.setImageResource(images[position%6]);
container.addView(iv);
return iv;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
}
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
private int prePos;
@Override
public void onPageSelected(int position) {
layout.getChildAt(position%6).setEnabled(false);//show blue
layout.getChildAt(prePos).setEnabled(true);//show hui
prePos=position%6;
}
});
}
class GuideAdapter extends PagerAdapter{
@Override
public int getCount() {
return 10000;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView iv=new ImageView(MainActivity.this);
iv.setScaleType(ScaleType.FIT_XY);
iv.setImageResource(images[position%6]);
container.addView(iv);
return iv;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
}
}