上午沒事寫了一篇,下午有事,晚上回來看看感覺寫的差點意思,上篇文章大概的關於循環是自己添加了兩個空的View,看到網上還有一種就是在自定義的Adapter中getCount中返回最大值,然後destroyItem不刪除View,添加異常,說了這麼多,我實驗之後總是有問題,如果有弄成功的可以告知我一下,我的是三張圖片~還是按照上午的那種方式就是第一個和最後一個是空View:
基礎布局
看下效果吧,加了一排小圈圈,比上午的那個Demo好看:
activity_pager.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/viewPager_pic"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/viewGroup_pic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
</FrameLayout>
實現Demo
自定義一個MyPicViewPagerAdapter:
private class MyPicViewPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return viewList.size();
}
@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) {
// TODO Auto-generated method stub
container.removeView(viewList.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
container.addView(viewList.get(position), 0);
return viewList.get(position);
}
}
activity中的字段:
private ViewPager viewPager;
private int[] srcArr = new int[] { R.drawable.eagle, R.drawable.heron,
R.drawable.flamingo };
private ViewGroup viewGroup;
private List<View> viewList;
private ImageView[] cycleList;
activity中的onCreate中實例化的ViewPager和ViewGroup,實例化的時候加入參數
viewPager = (ViewPager) findViewById(R.id.viewPager_pic);
viewGroup = (ViewGroup) findViewById(R.id.viewGroup_pic);
viewList = new ArrayList<View>();
viewList.add(new View(this));
for (int i = 0; i < srcArr.length; i++) {
ImageView imageView = new ImageView(this);
imageView.setBackgroundResource(srcArr[i]);
viewList.add(imageView);
}
viewList.add(new View(this));
// 加載小圓圈
cycleList = new ImageView[srcArr.length];
for (int i = 0; i < cycleList.length; i++) {
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new LayoutParams(10, 10));
cycleList[i] = imageView;<br> //0的時候選中
if (i ==0) {
cycleList[i].setBackgroundResource(R.drawable.page_indicator_selected);
} else {
cycleList[i].setBackgroundResource(R.drawable.page_indicator_unselected);
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
layoutParams.leftMargin = 5;
layoutParams.rightMargin = 5;
viewGroup.addView(imageView, layoutParams);
}
viewPager.setAdapter(new MyPicViewPagerAdapter());
viewPager.setCurrentItem(1);
Demo重要的是循環,由於是有五個View,三個小圈圈,所以就需要改變一下選中的setOnPageChangeListener:
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
for (int i = 0; i < cycleList.length; i++) {
cycleList[i].setBackgroundResource(R.drawable.page_indicator_unselected);
if (i == arg0-1) {
cycleList[i].setBackgroundResource(R.drawable.page_indicator_selected);
}
}
if(arg0 == 0)
viewPager.setCurrentItem(viewList.size()-2);
else if(arg0 == viewList.size() - 1)
viewPager.setCurrentItem(1);
}
@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
}
});