Android cyclic ViewPager (2)
I wrote an article in the morning, but I had something to do in the afternoon. I came back at night to see what I thought I was writing. In the previous article, I added two empty views to the loop, one other way on the Internet is to return the maximum value in getCount in the Custom Adapter. Then, destroyItem does not delete the View and adds an exception. After so many things are said, I always have problems after the experiment, if it succeeds, please let me know. I have three images ~ In the morning, the first and last one are empty views. The basic layout shows the effect and a small circle is added, which is better than the Demo in the morning: 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> allows the Demo to customize a 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) ;}} field in 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; ViewPager and ViewGroup instantiated in onCreate in activity. The parameter viewPager = (ViewPager) is added during instantiation) 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); // load the small circle export elist = new ImageView [srcArr. length]; for (int I = 0; I <cancelist. length; I ++) {ImageView imageView = new ImageView (this); imageView. setLayoutParams (new LayoutParams (10, 10); Specify elist [I] = imageView; <br> // if (I = 0) {specify elist [I]. setBackgroundResource (R. drawable. page_indicator_selected);} else {cancelist [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 is an important loop. Because there are five views and three small circles, you need to change the selected setOnPageChangeListener: viewPager. setOnPageChangeListener (new OnPageChangeListener () {@ Override public void onPageSelected (int arg0) {// TODO Auto-generated method stub for (int I = 0; I <cancelist. length; I ++) {cancelist [I]. setBackgroundResource (R. drawable. page_indicator_unselected); if (I = arg0-1) {cancelist [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 merge (int arg0) {// TODO Auto-generated method stub }});