Explain how to create Viewpager components in Android apps _android

Source: Internet
Author: User
Tags object object

Now a lot of apps are opened as a viewpager, then you can slide with your fingers, each slide once to change a picture, there will be a circle at the bottom to say that now slide to Chapter ~
Usually these pictures are to put the function of the introduction or use of teaching and so on, my demand is very simple, is mentioned above.
There are two ways, one is to find an existing suite, look up a bunch of data each and I recommend viewpagerindicator this set, I have seen this set, but it seems to need to have fragment plus Google Play example seems to be loaded, so have to do it yourself.
Viewpager's implementation can refer to the code in the Android Viewpager use details:

@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

  Setcontentview (R.layout.activity_intro);

  Mviewpager = (Viewpager) Findviewbyid (R.id.viewpager);

  Final Layoutinflater minflater = Getlayoutinflater (). from (this);
  View v1 = minflater.inflate (r.layout.intro_layout_1, NULL);
  View v2 = minflater.inflate (r.layout.intro_layout_2, NULL);
  View v3 = minflater.inflate (r.layout.intro_layout_3, NULL);

  View v4 = minflater.inflate (r.layout.intro_layout_4, NULL);
  Viewlist = new arraylist<view> ();
  Viewlist.add (v1);
  Viewlist.add (v2);
  Viewlist.add (v3);

  Viewlist.add (v4);
  Mviewpager.setadapter (New Myviewpageradapter (viewlist));
Mviewpager.setcurrentitem (0);}

  Myviewpageradapter public class Myviewpageradapter extends Pageradapter {private list<view> mlistviews;
  Public Myviewpageradapter (list<view> mlistviews) {this.mlistviews = Mlistviews; @Override public void Destroyitem (ViewGroup contaIner, int position, object object) {Container.removeview (View object); @Override public Object Instantiateitem (viewgroup container, int position) {View view = Mlistviews.get (Positi
    ON);
    Container.addview (view);
  return view;
  @Override public int GetCount () {return mlistviews.size ();
  @Override public boolean isviewfromobject (View arg0, Object arg1) {return arg0==arg1;

 }}

You're going to have a viewpager.


Let's take a specific look at the usage of Viewpager:
First, Viewpager create steps
① Add Android.support.v4.view.ViewPager to XML layout

② load the displayed page card to convert the layout layout to a View object

(1)

Layoutinflater LF Getlayoutinflater (this);
  Lf.inflate (Resource,root);

(2)

View.inflate (Context,resource,root);

③ Configuration adapter (three kinds of adapter)

(1) Pageradapter Data Source:list<view>
(2) Fragmentpageradapter Data Source:list<fragment>
(3) Fragmentstatepageradapter Data Source:list<fragment>
Second, the code example
new four fragment and their layout the following is the mainactivity XML layout:

<android.support.v4.view.viewpager
    android:id= "@+id/pager"
    android:layout_width= "Match_parent"
    android:layout_height= "Match_parent"
    android:layout_gravity= "center" >

    < Android.support.v4.view.PagerTabStrip
      android:id= "@+id/tab"
      android:layout_width= "Wrap_content"
      android:layout_height= "Wrap_content"
      android:layout_gravity= "Top" >
    </ Android.support.v4.view.pagertabstrip>
    <!--bottom display title and top display cannot appear at the same time
    < Android.support.v4.view.PagerTitleStrip
      android:id= "@+id/title"
      android:layout_width= "Wrap_content"
      android:layout_height= "wrap_content"
      android:layout_gravity= "Bottom" >
    </ android.support.v4.view.pagertitlestrip>
    -->
  </android.support.v4.view.ViewPager>

The first kind of pageradapter

public class Mypageradapter extends Pageradapter {private list<view> viewlist;

  Private list<string> titlelist;
    Public Mypageradapter (list<view> viewlist,list<string> titlelist) {this.viewlist = viewList;
  This.titlelist = titlelist;
  /** * Returns the number of page cards * * @Override public int GetCount () {return viewlist.size (); /** * View is from Object/@Override public boolean isviewfromobject (view arg0, Object arg1) {return arg0==
  Arg1; /** * Instantiate a page card */@Override public Object instantiateitem (viewgroup container, int position) {container
    . AddView (Viewlist.get (position));
  return Viewlist.get (position);

    /** * Destroy a page card */@Override public void Destroyitem (ViewGroup container, int position, object) {

  Container.removeview (Viewlist.get (position)); /** * Set Viewpager title * * @Override public charsequence getpagetitle (int position) {return titlelist.ge T (position);

 }

}

The second kind of fragmentpageradapter

public class Myfragmentpageradapter extends Fragmentpageradapter {
  private list<fragment> fraglist;
  Private list<string> titlelist;
  Public Myfragmentpageradapter (Fragmentmanager fm,list<fragment> fraglist,list<string> titleList) {
    Super (FM);
    This.fraglist = fraglist;
    This.titlelist = titlelist;
  }


  @Override public
  Fragment getitem (int arg0) {return

    fraglist.get (arg0);
  }

  @Override public
  charsequence getpagetitle (int position) {return

    titlelist.get (position);
  }

  @Override public
  int GetCount () {return

    fraglist.size ();
  }

}

The third type of fragmentstatepageradapter (this adapter can dynamically destroy fragment)

 public class MyFragmentPagerAdapter2 extends Fragmentstatepageradapter {//The adapter can dynamically destroy P
  Rivate list<fragment> fraglist;
  Private list<string> titlelist;
    Public MyFragmentPagerAdapter2 (Fragmentmanager fm,list<fragment> fraglist,list<string> titlelist) {
    Super (FM);
    This.fraglist = fraglist;
  This.titlelist = titlelist;
  @Override public Fragment getitem (int arg0) {return fraglist.get (arg0);
  @Override public charsequence getpagetitle (int position) {return titlelist.get (position);
  @Override public int GetCount () {return fraglist.size ();
  @Override public Object Instantiateitem (viewgroup arg0, int arg1) {return Super.instantiateitem (arg0, arg1); @Override public void Destroyitem (ViewGroup container, int position, object object) {Super.destroyitem (conta
  Iner, Position, object); }

}

Mainactivity use Getsupportfragmentmanager () the activity must inherit fragmentactivity:

public class Mainactivity extends fragmentactivity implements onpagechangelistener{private list<view> viewlist;
  Private list<string> titlelist;
  Private Viewpager Pager;

  Private Pagertabstrip tab;//The top of the title private list<fragment> fraglist;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main);
    Viewlist = new arraylist<view> ();
    Titlelist = new arraylist<string> ();

    tab = (Pagertabstrip) Findviewbyid (R.id.tab);
    View view1 = view.inflate (this,r.layout.view1,null);
    View view2 = view.inflate (this,r.layout.view2,null);
    View view3 = view.inflate (this,r.layout.view3,null);

    View view4 = view.inflate (this,r.layout.view4,null);
    Viewlist.add (View1);
    Viewlist.add (VIEW2);
    Viewlist.add (VIEW3);

    Viewlist.add (VIEW4);
    Fraglist = new arraylist<fragment> ();
    Fraglist.add (New Fragment1 ()); Fraglist.add (New FragmeNt2 ());
    Fraglist.add (New Fragment3 ());

    Fraglist.add (New Fragment4 ());
    Set the title Titlelist.add ("first page") for the Viewpager page card;
    Titlelist.add ("second page");
    Titlelist.add ("third page");
    Titlelist.add ("fourth page");
    Set some properties for Pagertabstrip Tab.setbackgroundcolor (color.white);
    Tab.setdrawfullunderline (FALSE);

    Tab.settabindicatorcolor (Color.Blue);
    Pager = (Viewpager) Findviewbyid (R.id.pager);
    Mypageradapter adapter = new Mypageradapter (viewlist,titlelist);
    Myfragmentpageradapter adapter = new Myfragmentpageradapter (Getsupportfragmentmanager (), fraglist, titleList); /** * Use Getsupportfragmentmanager () the activity must inherit fragmentactivity * * MyFragmentPagerAdapter2 adapter = new My
    FragmentPagerAdapter2 (Getsupportfragmentmanager (), fraglist, titlelist);
    Pager.setadapter (adapter);
  Pager.setonpagechangelistener (this);  @Override public void onpagescrollstatechanged (int arg0) {} @Override the public void onpagescrolled (int arg0, float arg1,int arg2) {} @Override public void onpageselected (int arg0) {Toast.maketext (this, "is currently the first" + (ARG0+1) + "interface", 0). s

  how ();
 }

}

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.