Flexible use of Android Actionbar and Viewpager switching pages _android

Source: Internet
Author: User
Tags gettext

This article is an example of Android using Actionbar and Viewpager to switch pages and share them for everyone's reference. Specifically as follows:
The screenshot of the running effect is as follows:

The project layout is as follows:

The specific code is as follows:

Mainactivity.java Code

Import java.util.ArrayList;
Import java.util.List;
Import Android.os.Bundle;
Import android.support.v4.app.Fragment;
Import Android.support.v4.app.FragmentManager;
Import Android.support.v4.app.FragmentPagerAdapter;
Import android.support.v4.app.FragmentTransaction;
Import Android.support.v4.view.ViewPager;
Import Android.support.v4.view.ViewPager.OnPageChangeListener;
Import Android.support.v7.app.ActionBar;
Import Android.support.v7.app.ActionBar.Tab;
Import Android.support.v7.app.ActionBar.TabListener;

Import android.support.v7.app.ActionBarActivity;
Import com.xuliugen.fragment.ChatFragment;
Import com.xuliugen.fragment.FriendFragment;
Import com.xuliugen.fragment.MomentFragment;

Import com.xuliugen.fragment.MoreFragment; public class Mainactivity extends Actionbaractivity implements Tablistener,onpagechangelistener {private List<mytab
  > tabslist = new arraylist<mainactivity.mytab> (4); Private Viewpager viewpager;//Viewpager for storing fragment private Actionbar ACTIOnBar;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

    Setcontentview (R.layout.activity_main);
    Add 4 fragment to the Custom tab to Tabslist.add (New Mytab ("chat", Chatfragment.class));
    Tabslist.add (New Mytab ("friend", Friendfragment.class));
    Tabslist.add (New Mytab ("moment", Momentfragment.class));

    Tabslist.add (New Mytab ("more", Morefragment.class));

  Initactionbar ();
    private void Initactionbar () {//find Viewpager Viewpager = (viewpager) This.findviewbyid (R.id.viewpager); Actionbar = Getsupportactionbar (); Get the Actionbar Actionbar.setnavigationmode (actionbar.navigation_mode_tabs) in V7;//set to navigation mode//To add the four tab created by looping to AC
      Tionbar for (Mytab tab:tabslist) {tab t = Actionbar.newtab ();//Use Tab V7 below T.settext (Tab.gettext ());
    T.settablistener (this);//Set Tablistener listener Actionbar.addtab (t); } viewpager.setadapter (New Tabfragmentpageradapter (getsupportfragmentmanageR ()));
  Viewpager.setonpagechangelistener (this); 
    }/** * Encapsulates a tab class * * TAB's TEXT * * tab contains class/class Mytab {private String text;//Caption text
    Private Class fragment;//the page corresponding to each tab fragment public String GetText () {return text;
    public void SetText (String text) {this.text = text;
    Public Class getfragment () {return fragment;
    The public void Setfragment (Class fragment) {this.fragment = fragment; /** * Two parameters are constructed to facilitate the creation of an object * * @param string * @param fragment/Public Mytab (string s
      Tring, Class fragment) {this.text = string;
    This.fragment = fragment; }/** * Set adapter for Viewpager/class Tabfragmentpageradapter extends Fragmentpageradapter {public tabfr
    Agmentpageradapter (Fragmentmanager FM) {super (FM); @Override public Fragment getitem (int i) {try {return (Fragment) Tabslist.get (i). Getfragment (). newinstance ();
      catch (Instantiationexception e) {e.printstacktrace ();
      catch (Illegalaccessexception e) {e.printstacktrace ();
    return null;
    @Override public int GetCount () {return tabslist.size (); }/************* below is the method implemented by Tablistener ***********************//** * When tab is checked twice/@Override public VO ID ontabreselected (Tab tab, Fragmenttransaction fragmenttransaction) {}/** * when Tab is selected * * * @Override Publ IC Void ontabselected (Tab tab, Fragmenttransaction fragmenttransaction) {Viewpager.setcurrentitem (Tab.getPosition ())
  ;  @Override public void ontabunselected (Tab tab, Fragmenttransaction fragmenttransaction) {}/**************** Below is the method implemented by Onpagechangelistener ****************/@Override public void onpagescrollstatechanged (int i) {} @Overri De public void onpagescrolled (int i, float arg1, int arg2) {} @Override public void Onpageselected (int i) {Actionbar.selecttab (Actionbar.gettabat (i));
 }

}

The bottom is four fragment.

public class Chatfragment extends Fragment {@Override public View oncreateview (Layoutinflater inflater, ViewGroup C
  Ontainer, Bundle savedinstancestate) {return inflater.inflate (R.layout.fragment_friends, container, false); } public class Friendfragment extends Fragment {@Override public View oncreateview (Layoutinflater inflater, VIEWG
  Roup container, Bundle savedinstancestate) {return inflater.inflate (R.layout.fragment_chat, container, false); } public class Momentfragment extends Fragment {@Override public View oncreateview (Layoutinflater inflater, Vie Wgroup container, Bundle savedinstancestate) {return inflater.inflate (r.layout.fragment_moment, container, Fals
  e); } public class Morefragment extends Fragment {@Override public View oncreateview (Layoutinflater inflater, Viewgro
  Up container, Bundle savedinstancestate) {return inflater.inflate (R.layout.fragment_more, container, false);

 }
}

Activity_main.xml

<relativelayout xmlns: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 "
  android:paddingbottom= "@dimen/activity_vertical_margin"
  android:paddingleft= "@dimen/activity_ Horizontal_margin "
  android:paddingright=" @dimen/activity_horizontal_margin "
  android:paddingtop=" @dimen /activity_vertical_margin "
  tools:context=". Mainactivity ">

  <android.support.v4.view.viewpager
    android:id=" @+id/viewpager "
    android: Layout_width= "Fill_parent"
    android:layout_height= "fill_parent" >
  </ Android.support.v4.view.viewpager>

</RelativeLayout>

Below are four fragment layout files

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:layout_width=" match_parent "
  android:layout_height=" fill_parent "
  android:o" rientation= "vertical" >

  <textview
    android:id= "@+id/textview" android:layout_width= "WRAP_"
    Content "
    android:layout_height=" wrap_content "
    android:layout_gravity=" Center_horizontal "
    android: text= "Chat" >
  </TextView>

</LinearLayout>

The other three are just the same as the filename and the contents of the inside change.

I hope this article will help you learn about Android software programming.

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.