Android based on Viewpager implementation of the application welcome Interface complete Example _android

Source: Internet
Author: User

This article describes the Android application welcome interface based on the Viewpager implementation. Share to everyone for your reference, specific as follows:

Sometimes the development of an application needs to guide users to suggest some new features, such a welcome interface implementation can use the method

First we need to use the Viewpager class, this class is in the Android-support-v4.jar, online search will have

The package is now placed in the project and then referenced

Viewpager kind of personal understanding is a container that puts the image resources you need into his adapter, so we need a adapter, a custom adapter inheritance Pageradapter, Then set the Viewpager setadapter () on it.

Next, the realization of small dots, prepare two pictures, one is checked the state, one is unchecked state, and then use selector set its change

The whole idea of the project is this, please see the following code:

public class Indexslide extends activity implements Onclicklistener, Onpagechangelistener {private Viewpager VP;
  Private Viewpageradapter Vpadapter;
  Private list<view> views;
  Boot picture Resources private static final int[] Pics = {R.drawable.chun, R.drawable.xia, R.drawable.qiu, R.drawable.dong};
  Bottom dot picture private imageview[] dots;
  Record the current selected location private int currentindex; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub super.oncreate
    (savedinstancestate);
    Setcontentview (R.layout.indexslide);
    views = new arraylist<view> ();
        Linearlayout.layoutparams mparams = new Linearlayout.layoutparams (LinearLayout.LayoutParams.WRAP_CONTENT,
    LinearLayout.LayoutParams.WRAP_CONTENT);
      Initializes a list of boot pictures for (int i = 0; i < pics.length i++) {ImageView IV = new ImageView (this);
      Iv.setlayoutparams (Mparams);
      Iv.setimageresource (Pics[i]);
    Views.add (iv);
 }   VP = (Viewpager) Findviewbyid (r.id.page);
    Initialize Adapter Vpadapter = new Viewpageradapter (views);
    Vp.setadapter (Vpadapter);
    Binding callback Vp.setonpagechangelistener (this);
  Initialize the bottom small point initdots ();
    private void Initdots () {LinearLayout LL = (linearlayout) Findviewbyid (R.ID.LL);
    Dots = new Imageview[pics.length];
      Loop to get the dot picture for (int i = 0; i < pics.length i++) {dots[i] = (ImageView) ll.getchildat (i);
      Dots[i].setenabled (TRUE);//all set to Gray dots[i].setonclicklistener (this);
    Dots[i].settag (i)//Set position tag, convenient to remove the corresponding with the current position} currentindex = 0; Dots[currentindex].setenabled (false);//set to White, selected state}/** * Set current boot page/private void Setcurview (int position)
    {if (Position < 0 | | | position >= pics.length) {return;
  } vp.setcurrentitem (position); /** * Set the current boot point to select/private void Setcurdot (int positon) {if (Positon < 0 | | positon > Pics.length -1 | | Currentindex = POsiton) {return;
    } dots[positon].setenabled (FALSE);
    Dots[currentindex].setenabled (TRUE);
  Currentindex = Positon; ///When the sliding state changes, call the public void onpagescrollstatechanged (int arg0) {//TODO auto-generated the method stub}//When the current page Call public void onpagescrolled (int arg0, float arg1, int arg2) {//TODO auto-generated Method stub}//When a new page is being slid
  The public void onpageselected (int arg0) {Setcurdot (arg0) is invoked when the face is selected;
    public void OnClick (View v) {int position = (Integer) v.gettag ();
    Setcurview (position);
  Setcurdot (position);
    Class Viewpageradapter extends Pageradapter {//Interface list private list<view> views;
    Public Viewpageradapter (list<view> views) {this.views = views; }//Destroy the interface of the arg1 location @Override public void Destroyitem (View arg0, int arg1, Object arg2) {(Viewpager) arg0
    ). Removeview (Views.get (arg1)); @Override public void Finishupdate (View arg0) {//TODO auto-generated method Stub}//Get the current number of interfaces @Override public int GetCount () {if (!= null) {RET
      Urn Views.size ();
    return 0; The interface @Override public Object instantiateitem (View arg0, int arg1) {(Viewpager) arg0) is initialized to the arg1 location. ADDV
      Iew (Views.get (arg1), 0);
    Return Views.get (ARG1); //Determine if the object generation interface @Override public boolean isviewfromobject (View arg0, Object arg1) {//TODO Auto-genera
    Ted Method Stub return (arg0 = = arg1); 
    @Override public void Restorestate (parcelable arg0, ClassLoader arg1) {//TODO auto-generated method stub
    @Override public parcelable SaveState () {//TODO auto-generated a stub return null;

 @Override public void Startupdate (View arg0) {//TODO auto-generated Method stub}}}

Layout file

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "> <android.support.v4.view .
  Viewpager android:id= "@+id/page" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" > </android.support.v4.view.ViewPager> <linearlayout android:id= "@+id/ll" android:orientation= "Horizontal "Android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:layout_marginbottom=" 24.0di P "android:layout_alignparentbottom=" true "android:layout_centerhorizontal=" true "> <imageview Andr
      Oid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center_vertical"
      Android:clickable= "true" android:padding= "15.0dip" android:src= "@drawable/dot"/> <imageview Android:layout_width= "Wrap_Content "android:layout_height=" wrap_content "android:layout_gravity=" center_vertical "android:clickable = "true" android:padding= "15.0dip" android:src= "@drawable/dot"/> <imageview android:layout_wid Th= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "Center_vertical" Android: Clickable= "true" android:padding= "15.0dip" android:src= "@drawable/dot"/> <imageview android:l
      Ayout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center_vertical"
Android:clickable= "true" android:padding= "15.0dip" android:src= "@drawable/dot"/> </LinearLayout>

 </RelativeLayout>

Set the selector of small dots

<?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
   <item android:state_enabled=" true "android:drawable=" @drawable/off "/>
  <item Android : state_enabled= "false" android:drawable= "@drawable/on"/>
</selector>

For more information on Android-related content readers can view the site: "Android View tips Summary", "Android Programming activity Operating Skills Summary", "Android operation SQLite Database Skills Summary", " Android operation JSON format data tips summary, "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", " Android Resource Operation tips Summary and the "Android Controls usage Summary"

I hope this article will help you with the Android program.

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.