Original link: http://www.orlion.ga/493/
First, fragments
Fragmentation (Fragment) is a UI fragment that can be embedded in an activity, which allows the program to make more reasonable and full use of the space on the large screen, so it is widely used on the tablet. Although fragmentation should be a whole new concept for you, I believe you should learn effortlessly, because it is so much like activity that it can also contain layouts and have its own life cycle. You can even think of fragmentation as a mini-activity, although this mini-activity is likely to be as big as normal activity.
So how do you use fragmentation to make the most of your flat screen space? Imagine that we are developing a news app, where one interface uses the ListView to display the title of a set of news, and when one of the headings is clicked, a different screen opens to show the details of the news. If you're designing on a phone, we can put a list of news headlines in one activity and put the details of the news in another activity.
But if this is also the case on the tablet, then the headline list will be stretched to fill the entire flat screen, and the headline of the news will not be too long, which would result in a large number of blank areas on the interface,
Therefore, a better design is to put the news headlines list interface and the news detail interface in two fragments, and then introduce the two fragments in the same activity, so that the screen space can be fully utilized,
Second, experience fragmentation layout
First create an Android project and then create the left fragment layout left_fragment.xml:
<?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 "android:orientation=" vertical " ><button android:id= "@+id/button" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_gravity= "Center_horizontal" android:text= "button"/></linearlayout>
Then create the right fragment layout right_fragment.xml:
<?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 "android:background=" #00ff00 " android:orientation= "vertical" > <textview android:layout_width= "wrap_content" Android:layout_heig ht= "Wrap_content" android:layout_gravity= "Center_horizontal" android:textsize= "20sp" android:text= "Th IS was right fragment "/></linearlayout>
Then create a new Leftfragment class that inherits from fragment:
Package Ga.orlion.fragmentdemo;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public class LeftFragment Extends Fragment {@Overridepublic View oncreateview (layoutinflater inflater, ViewGroup container, Bundle Savedinstancestate) {View view = Inflater.inflate (R.layout.left_fragment, container, false); return View;}}
This only overrides the fragment Oncreateview () method, and in this method the left_fragment layout that you just defined is dynamically loaded through the Layoutinflater inflate () method, Then create a rightfragment, code:
Package Ga.orlion.fragmentdemo;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public class RightFragment Extends Fragment {@Overridepublic View oncreateview (layoutinflater inflater, ViewGroup container, Bundle Savedinstancestate) {View view = Inflater.inflate (R.layout.right_fragment, container, false); return View;}}
Then modify the Activity_main.xml:
<?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= "Ga.orlion.fragmentdemo.LeftFragment" Android:layout_width= "0DP" android:layout_height= "Match_parent" android:layout_weight= "1"/> <fragment android:id= "@+id/right_ Fragment " android:name=" Ga.orlion.fragmentdemo.RightFragment " &Nbsp;android:layout_width= "0DP" android:layout_height= "Match_ Parent " android:layout_weight=" 1 "/></linearlayout>
Operation Result:
Iii. Dynamic addition of fragments
New Another_right_fragment.xml Code:
<?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 "android:background=" #ffff00 " android:orientation= "vertical" > <textview android:layout_width= "wrap_content" android:layout_height= "wrap _content "android:layout_gravity=" Center_horizontal "android:textsize=" 20sp "android:text=" This was another right Fragment "/></linearlayout>
Then create the anotherrightfragment:
Package Ga.orlion.fragmentdemo;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public Class Anotherrightfragment extends fragment{@Overridepublic View oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View view = Inflater.inflate (R.layout.another_right_fragment, container, false); return View;}}
The next step is to dynamically load the fragment into the activity. Modify Activity_main.xml:
<?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= "Ga.orlion.fragmentdemo.LeftFragment" Android:layout_width= "0DP" android:layout_height= "Match_parent" android:layout_weight= "1"/> <framelayout android:id= "@+id/right_ Layout " android:layout_width=" 0DP " android:layout_height= "Match_parent" android:layout_weight= "1" > <fragment android:id= "@+id/right_fragment" android:name= "Ga.orlion.fragmentdemo.RightFragment" android:layout_width= "Match_parent" android:layout_height= "Match_parent" /> </FrameLayout></LinearLayout>
The
framelayout does not have any positioning, and all the space is placed in the upper-left corner of the layout, as it is only needed to be placed as a fragment in the layout, so it is ideal for framelayout. We will then replace the content in the Framelayout in the code, enabling the ability to dynamically add fragmentation, modifying the code in Mainactivity as follows:
package ga.orlion.fragmentdemo;import android.app.activity;import android.app.fragmentmanager; Import android.app.fragmenttransaction;import android.os.bundle;import android.view.view;import android.view.view.onclicklistener;import android.widget.button;public class mainactivity extends activity implements onclicklistener{@Overrideprotected void oncreate ( bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main ); button button = (Button) findviewbyid (R.id.button); Button.setonclicklistener (this);} @Overridepublic void onclick (view v) {switch (V.getid ()) {case r.id.button: Anotherrightfragment fragment = new anotherrightfragment (); Fragmentmanager fragmentmanager = getfragmentmanager (); Fragmenttransaction transaction = fragmentmanager.begintransaction (); transAction.replace (r.id.right_layout, fragment); Transaction.commit (); break;default:break;}}}
You can see that we have registered a click event for the button in the left fragment and then put the logic for adding the fragments dynamically in the Click event. Dynamic add fragmentation is divided into 5 main steps:
1. Create a fragment instance 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.
Getting Started with Android (vi) fragments