Fragment, in a flat-panel application, see the view divided into two or more modules.
One, a simple fragment1. Create two local files to wait for the call to be called
(1) left_fragment
(2) Right_fragment
2. Create two classes that inherit from the Fragment class, respectively
(1) leftfragment (load XML file)
Public class extends Fragment { @Nullable @Override public View oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { View view = Inflater.inflate (R.layout.left_fragment,container,false ); //dynamic load XML return view;} }
(2) Rightfragment
I am the code (IBID.);
3. Adding <fragment> controls to XML called by activity
<?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="Match_parent"> <Fragment Android:ID="@+id/left_fragment" Android:name="Com.cenzhongman.myapplication.fragment.LeftleftFragment" Android:Layout_width="Wrap_content" Android:Layout_height="Match_parent" Android:Layout_weight="1"/> <Fragment Android:ID="@+id/right_fragment" Android:name="Com.cenzhongman.myapplication.fragment.RightFragment" Android:Layout_width="Wrap_content" Android:Layout_height="Match_parent" Android:Layout_weight="1"/></LinearLayout>
Two, dynamic switch fragment4. Create anotherrightfragment on top of original base (point to layout to invoke)
I am the code (IBID., less repetitive loading)
5. Change the layout method in the XML called by mainactivity
Use two framelayout (the layout that forces the upper left corner to align), to store fragment, Dynamic update time will change the content of Framelayout, namely its framelayout
<?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="Match_parent"> <Framelayout Android:ID="@+id/left_fragment" Android:Layout_width="Match_parent" Android:Layout_height="Match_parent" Android:Layout_weight="1"> <Fragment Android:name="Com.cenzhongman.myapplication.fragment.LeftleftFragment" Android:Layout_width="Match_parent" Android:Layout_height="Match_parent" Android:ID="@+id/fragment" /> </Framelayout> <Framelayout Android:ID="@+id/right_fragment" Android:Layout_width="Match_parent" Android:Layout_height="Match_parent" Android:Layout_weight="1"> <Fragment Android:name="Com.cenzhongman.myapplication.fragment.RightFragment" Android:Layout_width="Match_parent" Android:Layout_height="Match_parent" /> </Framelayout></LinearLayout>
6. Replace the fragment in the Mainactivity
public void OnClick (View v) { anotherrightfragment anotherrightfragment = new Anotherrightfragment (); Fragmentmanager Fragmentmanager = Getfragmentmanager (); Fragmenttransaction transaction = Fragmentmanager.begintransaction (); Transaction.replace (R.id.right_fragment, anotherrightfragment); Transaction.commit (); }
1. Create the fragment instance you want to add.
2. Get to Fragmentmanager, you can call the Getfragmentmanager () method directly in the activity.
3. Open a transaction by calling the BeginTransaction () method.
4. Adding fragments to the container, typically implemented using the Replace () method, requires the ID of the incoming container and the fragment to be added
Chip instance.
5. Commit the transaction and call the Commit () method to complete.
Third, add the fragment to the return stack
Transaction.addtobackstack (null);//Receive a name to describe the state of the return stack, usually passing in null
Creation and dynamic update of fragment fragments