Mainly the use of fragment, the steps to switch fragment are as follows
1, Get Fragmentmanager instance, but need mainactivity extends fragmentactivity to get Fragmentmanager.
Through Fragmentmanager Fragmentmanager=getsupportfragmentmanager ();
2, open transaction, a bit like the hibernate operation database in Java EE
Fragmenttranaction tranaction= fragmentmanager.begintranction ();
3. Replace fragment with transactions
Tranaction.replace (r.id.fl_main_content,fragment); The first one in the parameter represents the Fragmentlayout container, where the fragment is placed, and the second represents the fragment instance
4. Commit a transaction
Tranaction.commit ();
Here is a general description of today's main content:
1, create a base class Basepager, initialize the data inside, here first defines a view, as the test fragmentlayou display content.
2, create four sub-classes, inherit Basepager, respectively is Videopager,audiopager,netvideopager,netaudiopager
3, create a new set in the Mainactivity, in the activity of the OnCreate () in the collection instance new out, and added to the collection of four instances of subclasses.
4, through the Radiogroup Oncheckedchangedlistener () to monitor the status of RadioButton, to switch fragment
5, through the above listening class OnCheckedChanged method, to determine which RadioButton is selected, and then get the example, further to get the view, and then switch fragement to display different pages.
The specific code is as follows
1, Basepager
package com.yuanlp.mobileplayer.base;import android.content.context;import android.view.view;/* * * created by Yuriping on 2017/7/13. * base class */public abstract class BasePager { /** * This is the context, which is used after the subclass , so write for public * */ public final context context; public view rootview; public boolean isinitdata; public basepager (Context context) { this.context=context; rootview=initview (); } /** * force subclasses to achieve specific effects * @return */ public aBstract view initview (); /** * when subclasses are initialized, When you need to network request data, or when binding data, you need to rewrite the method */ public void InitData () { }}
2, Videopager, the other three will not write, very similar.
package com.yuanlp.mobileplayer.pager;import android.content.context;import android.graphics.color;import android.view.gravity;import android.view.view;import android.widget.textview;import com.yuanlp.mobileplayer.base.basepager;import com.yuanlp.mobileplayer.utils.logutil;/** * created by 原立鹏 on 2017/7/13. * Local Video page */public class VideoPager extends BasePager { public textview textview; public videopager (Context context) { super (context); } /** * forcing subclasses to achieve specific effects * * @return */ @Override public view initview ()  {        LOGUTIL.E ("Local video is initialized"); textview=new textview (context); textview.settextsize (+); textview.setgravity (Gravity.center); textview.settextcolor (color.red); return textview; } @Override public void initdata () { super.initdata ();      LOGUTIL.E ("The data for the local video page is initialized"); Textview.settext ("Local video page"); }}
3, Mainactivity
package com.yuanlp.mobileplayer.activity;import android.os.bundle;import android.support.annotation.idres;import android.support.annotation.nullable;import android.support.v4.app.fragment;import android.support.v4.app.fragmentactivity;import android.support.v4.app.fragmentmanager;import android.support.v4.app.fragmenttransaction;import android.view.layoutinflater;import android.view.view;import android.view.viewgroup;import Android.widget.framelayout;import android.widget.radiogroup;import com.yuanlp.mobileplayer.r;import com.yuanlp.mobileplayer.base.BasePager;import com.yuanlp.mobileplayer.pager.AudioPager;import Com.yuanlp.mobileplayer.pager.netaudiopager;import com.yuanlp.mobileplayer.pager.netvideopager;import com.yuanlp.mobileplayer.pager.videopager;import java.util.arraylist;import java.util.list;/** * Home * Created by Yuriping on 2017/7/12.&Nbsp;*/public class mainactivity extends fragmentactivity{ private FrameLayout fl_main_content; private RadioGroup rg_bottom_tag; /** * Local Page Collection */ public static list<basepager> basepagers; /** * Selected Location */ public static int position; @Override protected void oncreate (bundle savedinstancestate) { Super.oncreate (savedinstancestate); setcontentview ( R.layout.activity_main); fl_main_content= (FrameLayout) findviewbyid (r.id.fl_main_content); rg_bottom_tag= (Radiogroup) findviewbyid (R.id.rg_bottom_tag ); basepagers=new arraylist<basepager> (); basepagers.add (New videopager (this)); //Local video Location is 0 basepagers.add (New audiopager (this)); // Local music location is 1 basepagers.add (New netvideopager (this)); //Network Video location is 2 basepagers.add (new Netaudiopager (this)); //Network music location is 3 // Here set the Radiogroup monitor rg_bottom_tag.setoncheckedchangelistener (new myoncheckedchangelistener ()); rg_bottom_tag.check (R.id.rb _video) //Local video &nbs of the home page is selected by defaultp; } class myoncheckedchangelistener implements radiogroup.oncheckedchangelistener{ /** * <p>called when the checked radio button has changed. When the * selection is cleared, checkedid is -1.</p> * * @param group the group in which the checked radio button has changed * @param checkedId the unique identifier of the newly checked radio button */ @Override public Void oncheckedchanged (radiogroup group, @IdRes int checkedid) { switch (Checkedid) { default: position=0; break; case r.id.rb_audio: //selected local music position=1; break; case r.id.rb_video: //selected Local Video position=0; break; case r.id.rb_net_audio: //selected is the network music position=3; break; case r.id.rb_net_video: //selectedis the network video position=2; break; } //put the created page into fragment setfragment (); } private void setfragment () { Steps to //fragment &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//1, Get fragmentmanager fragmentmanager fragmentmanager = getsupportfragmentmanager (); &nBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//2, open transactions fragmenttransaction transaction = fragmentmanager.begintransaction (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//3, replacing fragement transaction.replace (R.id.fl_main_content,new myfragment ()); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//4, submit Transactions transaction.commit (); } } public static class myFragment extends Fragment{ @Nullable @Override &Nbsp; public view oncreateview (layoutinflater inflater, @Nullable viewgroup container, @Nullable bundle savedinstancestate) { basepager b1=getbasepager (); if (b1!=null) { return b1.rootview; } return null; } } public static basepager getbasepager () { basepager basepager=basepagers.get (position); if (Basepager!=null&&!basEpager.isinitdata) { basepager.isinitdata=true; Basepager.initdata (); //initialize page data, invoke InitData () method in each page class return basepager; } return basepager; }}
It is important to note that when replacing the Fragment step in Mainactivity, the second argument cannot write an anonymous inner class such as new Fragment () {}, otherwise it will be an error: Fragment null must be a public static class The reason for this error of properly recreated from is that the V4 package is upgraded to 25, fragment must be used as a static class for public. The specific use of the method can be referenced by my code.
This article is from the "Yuangushi" blog, make sure to keep this source http://cm0425.blog.51cto.com/10819451/1947356
Mobile Video The third day, by listening to the bottom Radiogroup button state to switch the contents of the middle Fragmentlayout