Android uses fragment to implement the Tab tab effect _android

Source: Internet
Author: User

Using fragment to Implement the Tab tab effect:

Will Radiogroup and fragment collection, implement Tab tab effect, here is the most critical of several files:

1.FragmentTabAdapter class:

/** * @Description: * @Author: Nate Robinson * @Since: 2015-2-12 * * public class Fragmenttabadapter implements Radiogrou P.oncheckedchangelistener {private list<fragment> fragments;//A tab page corresponds to a Fragment private radiogroup RGS; To switch tab private activity activity; Fragment belongs to the activity private int fragmentcontentid; The ID of the area to be replaced in the activity is private int currenttab; Current tab page index private Onrgsextracheckedchangedlistener Onrgsextracheckedchangedlistener; For the caller to add new functionality when switching tab fragmenttabadapter (activity, list<fragment> fragments, int
  Fragmentcontentid, Radiogroup rgs) {this.fragments = fragments;
  This.rgs = RGS;
  This.activity = activity;

  This.fragmentcontentid = Fragmentcontentid;
  Show first page by default android.app.FragmentTransaction ft = Activity.getfragmentmanager (). BeginTransaction ();
  Ft.add (Fragmentcontentid, Fragments.get (0));

  Ft.commit ();

 Rgs.setoncheckedchangelistener (this); @Override public void OnCheckedChanged (RadioGroup radiogroup, int checkedid) {for (int i = 0; i < Rgs.getchildcount (); i++) {if (Rgs.getchildat (i). GetId ()
    = = (Checkedid)) {Fragment Fragment = Fragments.get (i);
    Fragmenttransaction ft = Activity.getfragmentmanager (). BeginTransaction (); Getcurrentfragment (). OnPause (); Pauses the current tab//Getcurrentfragment (). OnStop ();  Pauses the current tab if (Fragment.isadded ()) {//Fragment.onstart ();//Start the Target tab OnStart () Fragment.onresume ();
    Start the Target tab onresume ()} else {Ft.add (Fragmentcontentid, fragment); } showtab (i);

    Show Target tab Ft.commit (); If you set the Toggle tab Extra feature interface if (null!= onrgsextracheckedchangedlistener) {Onrgsextracheckedchangedlistener.onrgsex
    Tracheckedchanged (Radiogroup, Checkedid, i); }}}/** * Toggle tab * @param idx/private void showtab (int idx) {for (int i = 0; i < fragments.si Ze ();
   i++) {Fragment Fragment = Fragments.get (i); Fragmenttransaction ft = ACTIVITY.GETFRAgmentmanager (). BeginTransaction ();
   if (idx = = i) {ft.show (fragment);
   else {ft.hide (fragment);
  } ft.commit (); } currenttab = idx;
 Update Target tab for current tab} public int Getcurrenttab () {return currenttab;
 Public Fragment getcurrentfragment () {return fragments.get (currenttab); Public Onrgsextracheckedchangedlistener Getonrgsextracheckedchangedlistener () {return onrgsextracheckedchangedlis
 Tener; } public void Setonrgsextracheckedchangedlistener (Onrgsextracheckedchangedlistener
 Onrgsextracheckedchangedlistener) {this.onrgsextracheckedchangedlistener = Onrgsextracheckedchangedlistener; /** * Toggle tab Extra function interface/public interface Onrgsextracheckedchangedlistener {void onrgsextracheckedchanged (Radio
 Group radiogroup, int checkedid, int index); 

 }
}

2.activity_main.xml layout file:

<?xml version= "1.0" encoding= "UTF-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:background=" @android: Color/white "android:orientation=" vertical "> <linearlayout android:layout_width=" fill_parent "Android:layout" _height= "fill_parent" android:orientation= "vertical" > <framelayout android:id= "@+id/tab_content" Android : layout_width= "fill_parent" android:layout_height= "0DP" android:layout_weight= "1.0" android:background= "#77557799 "/> <radiogroup android:id=" @+id/tabs_rg "android:layout_width=" fill_parent "android:layout_height=" Wra P_content "android:gravity=" center "android:orientation=" horizontal "android:paddingbottom=" 7DP "Android:paddi ngtop= "7DP" > <radiobutton android:id= "@+id/tab_rb_a" android:layout_width= "0DP" Android:layout_heig ht= "Wrap_content" android:layout_weight= "1.0" android:background= "@drawable/selector_tab" android:button= "@null" android:checked= "true" android:d
    rawabletop= "@drawable/tablatestalert" android:gravity= "center" android:singleline= "true" android:text= "TAB1" Android:textcolor= "#000000" android:textsize= "13sp"/> <radiobutton android:id= "@+id/tab_rb_b" an Droid:layout_width= "0DP" android:layout_height= "wrap_content" android:layout_weight= "1.0" android:background= " @drawable/selector_tab "android:button=" @null "android:drawabletop=" @drawable/tabsearch "android:gravity=" cent Er "android:singleline=" true "android:text=" Tab2 "android:textcolor=" #000000 "android:textsize=" 13SP "/&gt

   ;
    <radiobutton android:id= "@+id/tab_rb_c" android:layout_width= "0DP" android:layout_height= "Wrap_content" Android:layout_weight= "1.0" android:background= "@drawable/selector_tab" android:button= "@null" android:drawab letop= "@drawable/tABRECOMMD "android:gravity=" center "android:singleline=" true "android:text=" Tab3 "android:textcolor=" #0000

 "Android:textsize=" 13sp "/> </RadioGroup> </LinearLayout> </LinearLayout>

3.MainActivity class:

/** * @Description: * @Author: Nate Robinson * @Since: 2015-2-12 */public class Mainactivity extends activity {privat

 e Radiogroup RGS;

 Private list<fragment> fragments = new arraylist<fragment> ();

 private int index;

 private static Boolean Isexit = false;

 private static Boolean Hastask = false;

 Private RadioButton button;

 Private Timer texit = new timer ();
   Private TimerTask task = new TimerTask () {@Override public void run () {isexit = false;
  Hastask = true;

 }
 };

 Private Fragmenttabadapter Tabadapter;
  @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

  Setcontentview (R.layout.activity_main);
  Fragments.add (New Fragmentone ());
  Fragments.add (New Fragmenttwo ());

  Fragments.add (New Fragmentthree ());

  RGS = (radiogroup) Findviewbyid (R.ID.TABS_RG);

  button = (RadioButton) Findviewbyid (r.id.tab_rb_a);
  Tabadapter = new Fragmenttabadapter (this, fragments, r.id.tab_content, RGS); TaBadapter.setonrgsextracheckedchangedlistener (New Fragmenttabadapter.onrgsextracheckedchangedlistener () {@ Override public void onrgsextracheckedchanged (Radiogroup radiogroup, int checkedid, int index) {//Use this index to actually
    Now, if not on the homepage, press the first pass to let it first jump to the first fragment System.out.println ("Extra----" + index + "checked!!!");
   MainActivity.this.index = index;
 }
  }); @Override public boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_back) {if (i
     Ndex = = 0) {if (Isexit = = False) {Isexit = true;
     Toast.maketext (Mainactivity.this, "Press the exit procedure again", Toast.length_short). Show ();
     if (!hastask) {texit.schedule (task, 1000);
     } else {finish ();
    System.exit (0); } else {//if not on the home page, press the first pass to let it jump first to fragment//////////Rgs.check (BU
    Tton.getid ());
   Button.setchecked (TRUE);
 return false;
 }
}

To be able to learn the fragment lifecycle at the same time, I added log printing to each lifecycle method in fragment, one with three such fragment, I only posted one of the codes, and the rest was like this:

/** * @Description: * @Author: Nate Robinson * @Since: 2015-2-12 * * public class Fragmentone extends Basefragment {PR
 

 Ivate view view;
  @Override public void Onattach [activity activity] {Super.onattach (activity);
 LOG.D (TAG, "1==>fragmentone=>onattach");
  @Override public void OnCreate (Bundle savedinstancestate) {log.d (TAG, "2==>fragmentone==>oncreate");
 Super.oncreate (savedinstancestate); @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {Log.
  D (TAG, "3==>fragmentone==>oncreateview");
  View = Inflater.inflate (R.layout.fragment1, container, false);
  TextView TV = (TextView) View.findviewbyid (r.id.tv);
  Tv.settext ("Fragmentone");
 return view; @Override public void onactivitycreated (Bundle savedinstancestate) {log.d (TAG, "4==>fragmentone==>onactivit
  Ycreated ");
 Super.onactivitycreated (savedinstancestate); @Override public void OnStart () {log.d TAG,"5==>fragmentone==>onstart");
 Super.onstart ();
  @Override public void Onresume () {LOG.D (TAG, "6==>fragmentone==>onresume");
 Super.onresume ();
  @Override public void OnPause () {LOG.D (TAG, "7==>fragmentone==>onpause");
 Super.onpause ();
  @Override public void OnStop () {LOG.D (TAG, "8==>fragmentone==>onstop");
 Super.onstop ();
  @Override public void Ondestroyview () {LOG.D (TAG, "9==>fragmentone==>ondestroyview");
 Super.ondestroyview ();
  @Override public void OnDestroy () {LOG.D (TAG, "10==>fragmentone==>ondestroy");
 Super.ondestroy ();
  @Override public void Ondetach () {LOG.D (TAG, "11==>fragmentone==>ondetach");
 Super.ondetach (); @Override public void onviewcreated (view view, Bundle savedinstancestate) {log.d (TAG, "FRAGMENTONE==&GT;ONVIEWCR
  Eated ");
 super.onviewcreated (view, savedinstancestate);
 }
}

In the writing process, in order to achieve if not on the home page, press the first pass to let it jump first to the first fragment this effect, the beginning of the use of Rgs.check (Button.getid ()); In this case, the OnCheckedChanged listener event executes two times, then changes to button.setchecked (true), and this method successfully solves the problem.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.