Android UI Development 30th-building a flexible desktop with fragment

Source: Internet
Author: User

Absrtact: When we design the application, we hope to be able to make the most suitable for 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 ...

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 inside different layout file implementation, the following is a translation of the developer.android.com an article, the example can be seen in detail layout, layout-large and use fragmen to build a flexible desktop.

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 A

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

This is not like the

Android UI Development The 17th--android fragment instance puts the label in the layout file. Instead, use Fragmentmanager dynamic management fragment. Fragmentmanager Create a fragmenttransaction,It 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 () method

2. 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.

[HTML]View Plaincopy
    1. <framelayout < span class= "attribute" >xmlns:android= "http://schemas.android.com/apk/res/android"   
    2.     android:id=< Span class= "Attribute-value" > "@+id/fragment_container" &NBSP;&NBSP;
    3.      android:layout_width= "match_parent"   
    4.     android:layout_height= "match_parent"  />  
The activity uses Getsupportfragmentmanager () to get Fragmentmanager, and then calls BeginTransaction to create a Fragmenttransaction object, and then calls 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:

[Java]View Plaincopy
  1. Import Android.os.Bundle;
  2. Import android.support.v4.app.FragmentActivity;
  3. Public class Mainactivity extends Fragmentactivity {
  4. @Override
  5. public void OnCreate (Bundle savedinstancestate) {
  6. super.oncreate (savedinstancestate);
  7. Setcontentview (R.layout.news_articles);
  8. //Check The activity is using the layout version with
  9. //The Fragment_container framelayout
  10. if (Findviewbyid (r.id.fragment_container) = null) {
  11. //However, if we ' re being restored from a previous state,
  12. /Then we don ' t need to do anything and should return or else
  13. //We could end up with overlapping fragments.
  14. if (savedinstancestate! = null) {
  15. return;
  16. }
  17. //Create An instance of Examplefragment
  18. Headlinesfragment firstfragment = new Headlinesfragment ();
  19. This activity is started with special instructions from an Intent,
  20. //Pass the Intent ' s extras to the fragment as arguments
  21. Firstfragment.setarguments (Getintent (). Getextras ());
  22. //Add The fragment to the ' Fragment_container ' framelayout
  23. Getsupportfragmentmanager (). BeginTransaction ()
  24. . Add (R.id.fragment_container, firstfragment). commit ();
  25. }
  26. }
  27. }

The fragment here is added to the framelayout at run time, instead of directly using the tag definition in the activity's layout, the activity can either 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:

[Java]View Plaincopy
  1. Create fragment and give it an argument specifying the article it should show
  2. Articlefragment newfragment = new Articlefragment ();
  3. Bundle args = new bundle ();
  4. Args.putint (Articlefragment.arg_position, POSITION);
  5. Newfragment.setarguments (args);
  6. Fragmenttransaction transaction = Getsupportfragmentmanager (). BeginTransaction ();
  7. Replace whatever is in the Fragment_container view with this fragment,
  8. and add the transaction to the back stack so the user can navigate back
  9. Transaction.replace (R.id.fragment_container, newfragment);
  10. Transaction.addtobackstack (null);
  11. Commit the transaction
  12. Transaction.commit ();

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

Reference: http://developer.android.com/training/basics/fragments/fragment-ui.html

Figure II Google IO APP

/*** @author Zhang Xingye * http://blog.csdn.net/xyz_lmn* Android Development Advanced Group: 241395671*/

Android UI Development 30th-building a flexible desktop with fragment

Related Article

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.