Android Picture Carousel (simplest)

Source: Internet
Author: User

Layout file

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <framelayout android:layout_width= "Match_parent"Android:layout_height= "300dip" > <Android.support.v4.view.ViewPager Android:id= "@+id/vp"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"/> <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "35dip"android:layout_gravity= "Bottom"Android:background= "#33000000"android:gravity= "Center"android:orientation= "Vertical" > <TextView Android:id= "@+id/title"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Picture title"Android:textcolor= "@android: Color/white"/> <LinearLayout android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_margintop= "3dip"android:orientation= "Horizontal" > <View Android:id= "@+id/dot_0"Android:layout_width= "5dip"Android:layout_height= "5dip"Android:layout_marginleft= "2dip"Android:layout_marginright= "2dip"Android:background= "@drawable/dot_yes"/> <View Android:id= "@+id/dot_1"Android:layout_width= "5dip"Android:layout_height= "5dip"Android:layout_marginleft= "2dip"Android:layout_marginright= "2dip"Android:background= "@drawable/dot_no"/> <View Android:id= "@+id/dot_2"Android:layout_width= "5dip"Android:layout_height= "5dip"Android:layout_marginleft= "2dip"Android:layout_marginright= "2dip"Android:background= "@drawable/dot_no"/> <View Android:id= "@+id/dot_3"Android:layout_width= "5dip"Android:layout_height= "5dip"Android:layout_marginleft= "2dip"Android:layout_marginright= "2dip"Android:background= "@drawable/dot_no"/> <View Android:id= "@+id/dot_4"Android:layout_width= "5dip"Android:layout_height= "5dip"Android:layout_marginleft= "2dip"Android:layout_marginright= "2dip"Android:background= "@drawable/dot_no"/> </LinearLayout> </LinearLayout> </framelayout></relativ Elayout>

Use in a fragment

 PackageCom.example.administrator.yunphone.View;Importandroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;Importandroid.support.annotation.Nullable;ImportAndroid.support.v4.view.PagerAdapter;ImportAndroid.support.v4.view.ViewPager;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView;ImportCOM.EXAMPLE.ADMINISTRATOR.YUNPHONE.R;Importjava.util.ArrayList;Importjava.util.List;Importjava.util.concurrent.Executors;ImportJava.util.concurrent.ScheduledExecutorService;ImportJava.util.concurrent.TimeUnit;/*** Created by Administrator on 2016/7/13.*/ Public classHomefragmentextendsFragment {PrivateView MView; PrivateViewpager Mviewpaper; PrivateList<imageview>images; PrivateList<view>dots; Private intCurrentItem; //record the position of the last point    Private intoldposition = 0; //ID of the image to store    Private int[] Imageids =New int[]{r.drawable.home_bg, R.DRAWABLE.HOME_BG, R.DRAWABLE.HOME_BG, r.drawable    . HOME_BG, R.DRAWABLE.HOME_BG}; //title of the picture to be stored    PrivateString[] Titles =Newstring[]{"Carousel 1",            "Carousel 2",            "Carousel 3",            "Carousel 4",            "Carousel 5"    }; PrivateTextView title; PrivateViewpageradapter Adapter; PrivateScheduledexecutorservice Scheduledexecutorservice; @Nullable @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {MView=inflater.inflate (R.layout.fragment_home_layout,NULL);        Setview (); returnMView; }    Private voidSetview () {Mviewpaper=(Viewpager) Mview.findviewbyid (R.ID.VP); //the picture shownImages =NewArraylist<imageview>();  for(inti = 0; i < imageids.length; i++) {ImageView ImageView=NewImageView (Getactivity ());            Imageview.setbackgroundresource (Imageids[i]);        Images.add (ImageView); }        //small dots to showDots =NewArraylist<view>();        Dots.add (Mview.findviewbyid (R.ID.DOT_0));        Dots.add (Mview.findviewbyid (r.id.dot_1));        Dots.add (Mview.findviewbyid (r.id.dot_2));        Dots.add (Mview.findviewbyid (r.id.dot_3));        Dots.add (Mview.findviewbyid (r.id.dot_4)); Title=(TextView) Mview.findviewbyid (r.id.title); Title.settext (titles[0]); Adapter=NewViewpageradapter ();        Mviewpaper.setadapter (adapter); Mviewpaper.setonpagechangelistener (NewViewpager.onpagechangelistener () {@Override Public voidOnpageselected (intposition)                {Title.settext (titles[position]);                Dots.get (position). Setbackgroundresource (R.drawable.dot_yes);                Dots.get (oldposition). Setbackgroundresource (R.drawable.dot_no); Oldposition=position; CurrentItem=position; } @Override Public voidOnpagescrolled (intARG0,floatArg1,intarg2) {} @Override Public voidOnpagescrollstatechanged (intarg0)    {            }        }); }    /*the defined adapter*/     Public classViewpageradapterextendspageradapter{@Override Public intGetCount () {returnimages.size (); } @Override Public BooleanIsviewfromobject (View arg0, Object arg1) {returnarg0 = =arg1; } @Override Public voidDestroyitem (ViewGroup view,intposition, Object object) {            //TODO auto-generated Method Stub//Super.destroyitem (container, Position, object);//View.removeview (View.getchildat (position));//View.removeviewat (position);View.removeview (images.get (position)); } @Override PublicObject Instantiateitem (ViewGroup view,intposition) {            //TODO auto-generated Method StubView.addview (images.get (position)); returnImages.get (position); }    }    /*** Use thread pool to perform animation carousel timing*/@Override Public voidOnStart () {//TODO auto-generated Method Stub        Super. OnStart (); Scheduledexecutorservice=Executors.newsinglethreadscheduledexecutor (); Scheduledexecutorservice.schedulewithfixeddelay (NewViewpagetask (),2,                2, Timeunit.seconds); }    /*** Picture Carousel Task *@authorLiuyazhuang **/    Private classViewpagetaskImplementsrunnable{@Override Public voidrun () {CurrentItem= (CurrentItem + 1)%imageids.length; Mhandler.sendemptymessage (0); }    }    /*** Receive sub-line path handed over data*/    PrivateHandler Mhandler =NewHandler () { Public voidhandlemessage (android.os.Message msg) {Mviewpaper.setcurrentitem (CurrentItem);    };    }; @Override Public voidOnStop () {//TODO auto-generated Method Stub        Super. OnStop (); if(Scheduledexecutorservice! =NULL) {Scheduledexecutorservice.shutdown (); Scheduledexecutorservice=NULL; }    }}

Android Picture Carousel (simplest)

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.