private void initViewPager() {
advPager = (ViewPager) findViewById(R.id.img_activity_hotel_details_luobo);
ViewGroup group = (ViewGroup) findViewById(R.id.ll_activity_hotel_details__point_linear);
// 这里存放的是四张广告背景
List<View> advPics = new ArrayList<View>();
ImageView img1 = new ImageView(this);
img1.setBackgroundResource(R.drawable.img_home_page_advertisement);
advPics.add(img1);
ImageView img2 = new ImageView(this);
img2.setBackgroundResource(R.drawable.img_hotel_details);
advPics.add(img2);
ImageView img3 = new ImageView(this);
img3.setBackgroundResource(R.drawable.img_home_page_advertisement);
advPics.add(img3);
ImageView img4 = new ImageView(this);
img4.setBackgroundResource(R.drawable.img_hotel_details);
advPics.add(img4);
// 对imageviews进行填充
imageViews = new ImageView[advPics.size()];
// 小图标
for (int i = 0; i < advPics.size(); i++) {
imageView = new ImageView(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 0, 0, 0);
imageView.setLayoutParams(lp);
imageViews[i] = imageView;
if (i == 0) {
imageViews[i]
.setBackgroundResource(R.drawable.feature_point_cur);
} else {
imageViews[i].setBackgroundResource(R.drawable.feature_point);
}
group.addView(imageViews[i]);
}
advPager.setAdapter(new HomePageAdvViewAdapter(advPics));
advPager.setOnPageChangeListener(new GuidePageChangeListener());
advPager.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
isContinue = false;
break;
case MotionEvent.ACTION_UP:
isContinue = true;
break;
default:
isContinue = true;
break;
}
return false;
}
});}
private final class GuidePageChangeListener implements OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
what.getAndSet(arg0);
for (int i = 0; i < imageViews.length; i++) {
imageViews[arg0]
.setBackgroundResource(R.drawable.feature_point_cur);
if (arg0 != i) {
imageViews[i]
.setBackgroundResource(R.drawable.feature_point);
}
}
}
}
public class HomePageAdvViewAdapter extends PagerAdapter {
private List<View> views = null;
public HomePageAdvViewAdapter(List<View> views) {
this.views = views;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView(views.get(arg1));
}
@Override
public void finishUpdate(View arg0) {
}
@Override
public int getCount() {
return views.size();
}
@Override
public Object instantiateItem(View arg0, int arg1) {
((ViewPager) arg0).addView(views.get(arg1), 0);
return views.get(arg1);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {
}
}
使用ViewPager所需的jar下载地址:
http://download.csdn.net/detail/u011402761/7508213