Android uses Viewpager to start the app with the left and right swipe interface

Source: Internet
Author: User

Android uses Viewpager to start the app with the left and right swipe interface

/**

* 1. Load the Viewpager control in the layout Android.support.v4.view.ViewPager
* 2. Initialize Viewpager
* 3. Initialization of the data source
* 4. Create an adapter---> Inherit pageradapter override 4 methods
* 5. Bind Adapter
* 6. Setting Listener Events */

1. Load the Viewpager control in the layout Android.support.v4.view.ViewPager

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context=". Mainactivity " >    Add Viewpager layout     <Android.support.v4.view.ViewPager  android:id= "@+id/vpager"  A        Ndroid:background= "@drawable/guide_1"  android:layout_width= "Match_parent"  Android:layout_height= "match_parent" ></  Android.support.v4.view.ViewPager>         <ButtonAndroid:id= "@+id/enter"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentbottom= "true"Android:layout_centerhorizontal= "true"Android:layout_marginbottom= "20DP"Android:onclick= "OnEnter"android:visibility= "Gone"Android:text= "Enter" /></Relativelayout>

4. Create an adapter---> Inherit pageradapter override 4 methods
The main implementation of four methods

  

public int GetCount ()   //Gets the number of views loaded in the view
Determines whether a page view is determined to be related to the Instantiateitem returned by a particular focus object (Viewgroup,int). The method is a pageradapter normal need.
public boolean isviewfromobject (View arg0, Object arg1)  
Add a View

Destroying views
public void Destroyitem (ViewGroup container, int position, object object)
Public class Mypageradapter extends Pageradapter {private List<View>list; Public Mypageradapter (List<View>list)    {this.list = list;    } @Override public int getcount () {return list.size ();    } @Override public boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = Arg1; }/** * Add view */public Object Instantiateitem (viewgroup container, int position) {View V = list.get (        position);        Add the View object to the ViewGroup Container.addview (v);    return v; }/** * Remove view */public void Destroyitem (ViewGroup container, int position, object object) {container    . Removeview (List.get (position)); }        }

2. Initialize Viewpager
* 3. Initialization of the data source
* 5. Bind Adapter
* 6. Setting Listener Events

Public class Mainactivity extends Activity implements Onpagechangelistener {private Viewpager viewpager; Data source Private List<View>list;        Adapter private Mypageradapter Adapter;    Button Btn3;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Btn3 = (Button) Findviewbyid (r.id.enter);        Viewpager = (Viewpager) Findviewbyid (R.id.vpager); List = new ArrayList<View>();        Inindata ();        adapter = new Mypageradapter (list);        Bind adapter Viewpager.setadapter (adapter);        Set the monitoring Viewpager.setonpagechangelistener (this); /*new Handler (). postdelayed (New Runnable () {@Override public void run () {Intent in                 Tent = new Intent (mainactivity.this, One.class);                 StartActivity (Intent);            Finish ();        }}, (), */} public void Inindata () {ImageView iv1 = new ImageView (this);        Iv1.setbackgroundresource (r.drawable.guide_1);        List.add (IV1);        ImageView iv2 = new ImageView (this);        Iv2.setbackgroundresource (r.drawable.guide_2);        List.add (IV2);        ImageView iv3 = new ImageView (this);        Iv3.setbackgroundresource (R.drawable.guide_3);        List.add (IV3);        ImageView iv4 = new ImageView (this);        Iv4.setbackgroundresource (R.drawable.guide_4);    List.add (IV4); } public void OnEnter (View V) {new Handler (). Post (new Runnable () {@Override public void run () {Intent                 Intent = new Intent (mainactivity.this, one.class);                 StartActivity (Intent);            Finish ();    }        });     }/** * There are three states (0,1,2).     * Arg0 ==1 's time is sliding, * the arg0==2 of the hour of the implied sliding, * arg0==0 of the hour implied that nothing has been done.    */public void onpagescrollstatechanged (int arg0) {log.i ("tag", "onpagescrollstatechanged" +arg0); }/** * arg0: Current page, and the page you clicked to swipe arg1: percent of current page offset arg2: Pixel position of the current page offset */public void onpagescrolled (i    NT arg0, float arg1, int arg2) {log.i ("tag", "onpagescrolled" +arg0+ "--" +arg1+ "--->" +arg2);     }/** * This method is called after the page has been jumped, * arg0 is the position (position number) of the page you are currently selecting.        */public void onpageselected (int arg0) {log.i ("tag", "onpageselected" +arg0);        if (arg0 = = 3) {btn3.setvisibility (view.visible); }    }}

Use Viewpager to display the upper ad campaign, add an asynchronous task,

Use Setcurrentitem (index) to set the current page

Class Myasynctask extends Asynctask<Void, void, void>{protected void Doinbackground (void ... params) {while (true) {//Determines whether the current asynchronous task is canceled                if (iscancelled ()) {break;                    } try {thread.sleep (2000);                                    Publishprogress ();                } catch (Exception e) {e.printstacktrace ();        }} return null;            } protected void onprogressupdate (void ... values) {super.onprogressupdate (values);                Viewpager.setcurrentitem (++index%4);                    if (index==4) {Intent Intent = new Intent (mainactivity.this, One.class);                    StartActivity (Intent);                    Finish ();                    index=0;                                        Viewpager.removeallviews ();                    Viewpager.setcurrentitem (0);                  Add ();              }        }    } 

Android uses Viewpager to start the app with the left and right swipe interface

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.