It is my simple understanding that fragmentation (Fragment) is embedded with the UI fragments in the activity and exists for a reasonable allocation of the layout. It is used for both mobile and tablet UI, and also for flexible and advanced UI authoring.
Demo Simple button switch two different demo
New Left_fragment.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:orientation=" vertical "android:layout_width=" match_parent " android:layout _height= "Match_parent" > <button android:id= "@+id/button" android:layout_ Width= "Match_parent" android:layout_height= "Wrap_content" android:layout_gravity = "Center_horizontal" android:text= "button" /></linearlayout>
New Right_fragment.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:background=" #00ff00 " android:orientation=" vertical "Android:layout_ Width= "Match_parent" android:layout_height= "Match_parent" ><TextView android:layout_ Width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_gravity = "Center_horizontal" android:textsize= "20SP" android:text= "This was right Fragment "/></linearlayout>
New Another_right_fragment.xml
<?xml version= "1.0" encoding= "Utf-8"? ><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= "#ffff00" > <TextView android: Layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_ Gravity= "Center_horizontal" android:textsize= "20SP" android:text= "This is Right fragment "/></linearlayout>
Main_activity.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/activity_main"android:orientation= "Horizontal"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <Fragment Android:layout_width= "0DP"Android:name= "Test.example.com.fragmenttest.LeftFragment"Android:layout_height= "Match_parent"Android:layout_weight= "1"Android:id= "@+id/left_fragment"/> <framelayout Android:id= "@+id/right_layout"Android:layout_width= "0DP"Android:layout_height= "Match_parent"Android:layout_weight= "1" ></FrameLayout></LinearLayout>
Create the corresponding class separately
Packagetest.example.com.fragmenttest;Importandroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;/*** Created by hs769 on 2017/4/4.*/ Public classLeftfragmentextendsFragment {@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View View=inflater.inflate (R.layout.lift_fregment,container,false); returnview; }}
Packagetest.example.com.fragmenttest;//import android.app.Fragment;Importandroid.support.v4.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;/*** Created by hs769 on 2017/4/4.*/ Public classRightfragmentextendsfragment{@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View View=inflater.inflate (R.layout.right_fragment,container,false); returnview; }}
Packagetest.example.com.fragmenttest;Importandroid.support.v4.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;/*** Created by hs769 on 2017/4/4.*/ Public classAnotherrightfragmentextendsFragment {@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View View=inflater.inflate (R.layout.another_right_fragment,container,false); returnview; }}
Packagetest.example.com.fragmenttest;Importandroid.support.v4.app.Fragment;ImportAndroid.support.v4.app.FragmentManager;Importandroid.support.v4.app.FragmentTransaction;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button; Public classMainactivityextendsAppcompatactivityImplementsview.onclicklistener{@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button Button=(Button) Findviewbyid (R.id.button); Button.setonclicklistener ( This); Replacefragment (Newrightfragment ()); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.button:replacefragment (Newanotherrightfragment ()); Break; default: Break; } } Private voidreplacefragment (Fragment Fragment) {Fragmentmanager Fragmentmanager=Getsupportfragmentmanager (); Fragmenttransaction Transaction=fragmentmanager.begintransaction (); Transaction.replace (r.id.right_layout,fragment); Transaction.commit (); }}
Leftfragment,rightfragment and Another_right_fragment These three classes are extends ( inherited)fragment class respectively, this is a key, Since there are two packages containing Fragment, it is recommended to choose Android.support.v4.app.Fragment
If the package selection is not the same as the following error (Mainacitvity.java), you can change: Find the problem class, replace the package, complete
The final effect realizes click button Toggle Fragmentation (two fragment for changes before and after a click)
Android UI: Bottom menu bar learning and making--fragment fragment one