Android UI Development-use fragment to build a flexible desktop

Source: Internet
Author: User

When we design the application, we hope to be able to do our best to fit a variety of devices, including 4-inch screen, 7-inch screen, 10-inch screen and so on, Android development documentation for our reference, and Google Io app (ii) also realized this idea, they are using layout, Layout-large is implemented in different layout files. When designing applications, you can reuse fragment in different layout structures to support numerous screen sizes, optimizing the user experience on available screen space. For example, on a handheld device (such as a Nexus 4), a screen displays a fragment, and multiple fragment can be used to display information on a larger screen, such as a Nexus 7. Such as:

Figure one, in the large screen two fragment display in a screen, but the handheld device, need two screens display, one screen can only show one, they need to guide each other.

The Fragmentmanager class provides methods that allow you to add, delete, and replace fragment while the activity is running to create a flexible, dynamic experience.

add fragment to a running activity

The Fragmentmanager dynamic management fragment is used here. Fragmentmanager creates an fragmenttransaction that provides APIs for adding, deleting, and other fragment transactions. Activity allows removal or replacement of fragment requires the following conditions:

1. Add the initialized fragment to the activity's onCreate () method2. There must be a view container in the layout of the fragment placement

In the program example, the Res/layout/news_articles.xml file provides a view container.

1  <framelayout xmlns:android= "Http://schemas.android.com/apk/res/android"2      android:id= "@+id/ Fragment_container "3      android:layout_width=" Match_parent "4      android:layout_height=" Match_parent "/>

 

Use Getsupportfragmentmanager () in activity to get Fragmentmanager, Then call BeginTransaction to create a Fragmenttransaction object, and then call the Add () method to add a fragment. in activity, you can use the same Fragmenttransaction object to perform multiple fragment transactions, and you must call the Commint () method when doing so. The following code shows how to add a fragment to res/layout/news_articles.xml layout:

1 ImportAndroid.os.Bundle;2 Importandroid.support.v4.app.FragmentActivity;3 4  Public classMainactivityextendsfragmentactivity {5 @Override6      Public voidonCreate (Bundle savedinstancestate) {7         Super. OnCreate (savedinstancestate);8 Setcontentview (r.layout.news_articles);9 Ten         //Check The activity is using the layout version with One         //The fragment_container framelayout A         if(Findviewbyid (r.id.fragment_container)! =NULL) { -  -             //However, if we ' re being restored from a previous state, the             //Then we don ' t need to do anything and should return or else -             //we could end up with overlapping fragments. -             if(Savedinstancestate! =NULL) { -                 return; +             } -  +             //Create An instance of Examplefragment AHeadlinesfragment firstfragment =Newheadlinesfragment (); at              -             //The This activity is started with special instructions from an Intent, -             //Pass the Intent ' s extras to the fragment as arguments - firstfragment.setarguments (Getintent (). Getextras ()); -              -             //ADD The fragment to the ' Fragment_container ' framelayout in Getsupportfragmentmanager (). BeginTransaction () - . Add (R.id.fragment_container, firstfragment). commit (); to         } +     } -}


The fragment here is added to Framelayout at run time, instead of directly using the <fragment> tag definition in the activity's layout, the activity can remove it or replace it with a different fragment.

Replace fragment

The process of replacing a fragment is similar to adding a fragment, but requires the replace () method instead of the Add () method. It is important to note that when executing a fragment transaction, such as replacing or deleting a fragment, if you want to be able to fall back to the current, you must call the Addtobackstack () method before you commit the fragment transaction.

When a transaction is added to the stack when a fragment is removed or replaced, the removed Fragmeng is not extinct and fragment restarts if the user returns. If it is not put into the stack, when fragment is replaced or removed, the fragment dies.

Here is an example of replacing fragment:

1 //Create fragment and give it an argument specifying the article it should show2Articlefragment newfragment =Newarticlefragment ();3Bundle args =NewBundle ();4 Args.putint (articlefragment.arg_position, POSITION);5 newfragment.setarguments (args);6 7Fragmenttransaction transaction =Getsupportfragmentmanager (). BeginTransaction ();8 9 //Replace Whatever is in the Fragment_container view with this fragment,Ten //and add the transaction to the back stack so the user can navigate back One transaction.replace (R.id.fragment_container, newfragment); ATransaction.addtobackstack (NULL); -  - //Commit The transaction theTransaction.commit ();

The Addtobackstack () method has an optional string parameter that specifies a unique name for the transaction, which is a non-essential.

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.