Android Loop Viewpager (ii)

Source: Internet
Author: User

I wrote an article in the morning, something in the afternoon, back at night to see the feeling of writing almost, the last article about the loop is to add two empty view, see online There is also a custom adapter in the GetCount in the return of the maximum value, Then destroyitem do not delete the view, add the exception, said so much, I always have problems after the experiment, if there is success can tell me, my is three pictures ~ or in the way of the morning is the first and the last is empty view:

Basic Layout

Look at the effect, add a row of small circles, than the morning of the demo look good:

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_con Tent "/> <relativelayout android:layout_width=" fill_parent "android:layout_height=" Wra P_content "android:orientation=" vertical "> <linearlayout android:id=" @+id/viewgroup _pic "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "Andro Id:layout_alignparentbottom= "true" android:layout_marginbottom= "30DP" android:gravity= "Center_hor Izontal "android:orientation=" horizontal "> </LinearLayout> </RelativeLayout> & Lt;/framelayout>
Implement demo

Customizing a Mypicviewpageradapter:

Private class Mypicviewpageradapter extends Pageradapter {@Overridepublic int getcount () {//TODO auto-generated Method St Ubreturn viewlist.size ();} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {//TODO auto-generated method Stubreturn arg0 = arg1;} @Overridepublic void Destroyitem (ViewGroup container, int position, object object) {//TODO auto-generated method Stubcont Ainer.removeview (Viewlist.get (position));} @Overridepublic Object Instantiateitem (viewgroup container, int position) {//TODO auto-generated method Stubcontainer.addview (Viewlist.get (position), 0); return viewlist.get (position);}}

Fields in activity:

Private Viewpager viewpager;private int[] Srcarr = new int[] {r.drawable.eagle, R.drawable.heron,r.drawable.flamingo};p Rivate viewgroup viewgroup;private list<view> viewlist;private imageview[] cyclelist;

Viewpager and ViewGroup instantiated in OnCreate in activity, adding parameters when instantiated

Viewpager = (Viewpager) Findviewbyid (r.id.viewpager_pic); viewgroup = (ViewGroup) Findviewbyid (R.id.viewGroup_pic); Viewlist = new Arraylist<view> (), Viewlist.add (new View), for (int i = 0; i < srcarr.length; i++) {ImageView ImageView = new ImageView (this); Imageview.setbackgroundresource (Srcarr[i]); Viewlist.add (ImageView);} Viewlist.add (new View);//load small Circle cyclelist = new Imageview[srcarr.length];for (int i = 0; i < cyclelist.length; i++ {ImageView ImageView = new ImageView (this); Imageview.setlayoutparams (new Layoutparams (Ten)); Cyclelist[i] = ImageView;
When 0, select 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 important is the loop, because there are five view, three small circles, so you need to change the selected Setonpagechangelistener:

Viewpager.setonpagechangelistener (New Onpagechangelistener () {@Overridepublic void onpageselected (int arg0) {//TODO Auto-generated method Stubfor (int i = 0; i < cyclelist.length; i++) {Cyclelist[i].setbackgroundresource (R.DRAWABLE.PA ge_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);} @Overridepublic void onpagescrolled (int arg0, float arg1, int arg2) {//TODO auto-generated method stub} @Overridepublic vo ID onpagescrollstatechanged (int arg0) {//TODO auto-generated Method stub}});

Finally, look at the following:

Android Loop Viewpager (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.