The use of Viewpager and fragment in Android _android

Source: Internet
Author: User
Tags static class

Small case

in XML

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

Create fragment

 Fragments = new arraylist<> ();
 Conversationfragment fragment1 = new Conversationfragment ();
 Groupfragment Fragment2 = new Groupfragment ();
 Searchfragment Fragment3 = new Searchfragment ();
 Fragments.add (fragment1);
 Fragments.add (Fragment2);
 Fragments.add (FRAGMENT3);
 adapter = new Mainpageradapter (Getsupportfragmentmanager (), fragments);
 Viewpager.setadapter (adapter);

Adapter

public class Mainpageradapter extends fragmentpageradapter{

  list<fragment> fragmentlist;

  Public Mainpageradapter (Fragmentmanager FM, list<fragment> fragmentlist) {
    super (FM);
    This.fragmentlist = fragmentlist;
  }

  @Override public
  Fragment getitem (int position) {return
    fragmentlist.get (position);
  }

  @Override public
  int GetCount () {return
    fragmentlist.size ();
  }
}

Onpagechangelistener

Viewpager.addonpagechangelistener (New Viewpager.onpagechangelistener () {
  @Override public
  Void onpagescrolled (int position, float positionoffset, int positionoffsetpixels) {
    int distance = positionoffsetpixels/ 3;
    Once the fragment slide, the position here is actually the previous
    viewpropertyanimator.animate (v_indicate_line). Translationx (distance + Position * V_indicate_line.getwidth ()). Setduration (0);
  }

  @Override public
  void onpageselected (int position) {
    textlightandsize ();
  }


  @Override public
  void onpagescrollstatechanged (int state) {

  }
});

With other click events

Here is the note setcurrentitem usage
switch (View.getid ()) {case
  r.id.ly_conversation:
    Viewpager.setcurrentitem (0);
    break;
  Case R.id.ly_group:
    viewpager.setcurrentitem (1);
    break;
  Case R.id.ly_search:
    viewpager.setcurrentitem (2);
    break;


Official case

R.layout.fragment_pager

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:padding= "4dip" android:gravity= "Center_horizontal" android:layout_width= "Match_parent" Android:layout_hei ght= "Match_parent" > <android.support.v4.view.viewpager android:id= "@+id/pager" Match_parent "android:layout_height=" 0px "android:layout_weight=" 1 "> </android.support.v4.view.viewpag er> <linearlayout android:orientation= "horizontal" android:gravity= "center" android:measurewithlargestchild 
    = "true" android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:layout_weight= "0" > <button android:id= "@+id/goto_first" android:layout_width= wrap_content "android:layout_height=" wrap_content "android:text=" @string/first > </Button> <button android:id= "@+id/goto_last" Android:layo Ut_width= "Wrap_content" Android:layout_height= "Wrap_content" android:text= "@string/last" > </Button> </LinearLayout> </linearlay

 Out>

R.layout.fragment_pager_list

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" Android:layout_width= "Match_parent" android:layout_height= "match_parent" android:background= "@android:d rawable/ Gallery_thumb "> <textview android:id=" @+id/text "android:layout_width=" Match_parent "android:layout_height=" Wrap_content "android:gravity=" Center_vertical|center_horizontal "android:textappearance="? android:attr/ Textappearancemedium "android:text=" @string/hello_world "/> <!--the frame layout is-here since we'll be sho Wing either the empty view or the list view. --> <framelayout android:layout_width= "match_parent" android:layout_height= "0dip" Android:layout_weigh t= "1" > <!--here is the list. 
    Since we are using a listactivity, we have to called it "@android: id/list" So listactivity'll find it--> <listview android:id= "@android: Id/list" android:layout_width= "Match_pareNT "android:layout_height=" Match_parent "android:drawselectorontop=" false/> <!--here is the view To show if the list is emtpy--> <textview android:id= "@android: Id/empty" android:layout_width= "Match_pare NT "android:layout_height=" Match_parent "android:textappearance="? Android:attr/textappearancemedium "and roid:text= "No items."

 /> </FrameLayout> </LinearLayout>

public class Fragmentpagersupport extends fragmentactivity {static final int num_items = 10;

  Myadapter Madapter;

  Viewpager Mpager;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

    Setcontentview (R.layout.fragment_pager);

    Madapter = new Myadapter (Getsupportfragmentmanager ());
    Mpager = (Viewpager) Findviewbyid (R.id.pager);

    Mpager.setadapter (Madapter);
    Watch for button clicks.
    Button button = (button) Findviewbyid (R.id.goto_first);
      Button.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {mpager.setcurrentitem (0);
    }
    });
    Button = (button) Findviewbyid (r.id.goto_last); Button.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {Mpager.setcurrentitem (num_it
      EMS-1);
  }
    }); } public static class Myadapter extends Fragmentpageradapter {public myadapter (fragmentmanager FM) {Super (F
 m);   @Override public int GetCount () {return num_items;
    @Override public Fragment getitem (int position) {return arraylistfragment.newinstance (position);

    } public static class Arraylistfragment extends listfragment {int mnum;
     /** * Create A new instance of countingfragment, providing "num" * as an argument.

      */static arraylistfragment newinstance (int num) {arraylistfragment f = new arraylistfragment ();
      Supply num input as an argument.
      Bundle args = new Bundle ();
      Args.putint ("num", num);

      F.setarguments (args);
    return F;
     /** * When creating, retrieve this instance ' s number from its arguments.
      * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Mnum = getarguments ()!= null?
    Getarguments (). GETINT ("num"): 1; }/** * The Fragment ' s UI is just a simple text view showing its
     * Instance number. * * @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancest
      ATE) {View v = inflater.inflate (R.layout.fragment_pager_list, container, false);
      View TV = V.findviewbyid (R.id.text);
      ((TEXTVIEW) TV). SetText ("Fragment #" + mnum);
    return v; @Override public void onactivitycreated (Bundle savedinstancestate) {super.onactivitycreated (Savedinstanc
      Estate); Setlistadapter (New arrayadapter<string> (getactivity), Android.
    R.layout.simple_list_item_1, cheeses.scheesestrings)); @Override public void Onlistitemclick (ListView l, View v, int position, long id) {LOG.I ("fragmentlist",
    "Item clicked:" + ID);

 }
  }
}

Attention

Activity before 3.0 cannot be fragment. In order to be able to use fragment (SUPPORTV4), only fragmentactivity. An activity inherited by Fragmentactivity.

This is the entire content of this article, I hope to learn more about Android software programming help.

Related Article

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.