One example is to explain how fragment achieves different la s

Source: Internet
Author: User

I personally think fragment is very flexible and useful, especially for devices with large screen sizes such as tablets. If you are not familiar with fragment, please read this article () instead of getfragmentmanager (). the other article I recommended is enough. Well, let's take a look at the project effect. (why didn't I respond when I made a dynamic graph clearly)

The above is suitable for entering details from the list. Let's take a look at the layout of the horizontal and vertical screens.

1. vertical screen layout. We use framelayout, which contains two linearlayout containers for fragment. This layout is the focus.

<Framelayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "match_parent"> <! -- Load the fragment container of the list --> <linearlayout Android: Id = "@ + ID/list_container" Android: layout_width = "match_parent" Android: layout_height = "match_parent"> </linearlayout> <! -- Load the details of the fragment container --> <linearlayout Android: Id = "@ + ID/detail_container" Android: layout_width = "match_parent" Android: layout_height = "match_parent"> </linearlayout> </framelayout>

2. the horizontal screen layout is relatively simple. We create a layout-land folder under Res and place a layout folder with the same name as above.

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "match_parent"> <! -- Mount the fragment container of the list --> <linearlayout Android: Id = "@ + ID/list_container" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: layout_weight = "2"> </linearlayout> <! -- Load the details of the fragment container --> <linearlayout Android: Id = "@ + ID/detail_container" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: layout_weight = "1"> </linearlayout>

3. Create a new list fragment and run the Code directly. There is only one listview in it to display the list data.

Package COM. example. fragmentdemo3; import Java. util. arraylist; import Java. util. list; import android. content. res. configuration; import android. OS. bundle; import android. support. v4.app. fragment; import android. support. v4.app. fragmentmanager; import android. support. v4.app. fragmentmanager. onbackstackchangedlistener; import android. support. v4.app. fragmenttransaction; import android. view. layoutinflater; import Droid. view. view; import android. view. viewgroup; import android. widget. adapterview; import android. widget. adapterview. onitemclicklistener; import android. widget. arrayadapter; import android. widget. listview; import android. widget. toast;/*** list fragment * @ author mining **/public class fragmentlist extends fragment {private list <string> m1_celist = new arraylist <string> (); Private list <fragmenttransa Ction> mbackstacklist = new arraylist <fragmenttransaction> (); @ overridepublic view oncreateview (layoutinflater Inflater, viewgroup container, bundle savedinstancestate) {return Inflater. inflate (R. layout. fragment_list_layout, container, false) ;}@ overridepublic void onactivitycreated (bundle savedinstancestate) {super. onactivitycreated (savedinstancestate); // Add data to listviewfor (INT I = 0, Count = 20; I <count; I ++) {mdatasourcelist. add ("list data" + I);} // listviewlistview listview = (listview) getactivity () on the list page (). findviewbyid (R. id. fragment_list); listview. setadapter (New arrayadapter (getactivity (), android. r. layout. simple_list_item_1, m1_celist); listview. setonitemclicklistener (New onitemclicklistener () {@ overridepublic void onitemclick (adapterview <?> Parent, view, int position, long ID) {// instantiate details fragmentfragment detailfragment = new fragmentdetail (); // pass the required parameters from the list page to the details page. Bundle mbundle = new bundle (); mbundle. putstring ("Arg", mdatasourcelist. get (position); detailfragment. setarguments (mbundle); Final fragmentmanager = getactivity (). getsuppfrfragmentmanager (); Final fragmenttransaction = fragmentmanager. begintransaction (); // determines whether the screen configuration of the mobile phone is getactivity (). getresources (). getconfiguration (); int Ori = configuration. orientation; fragmenttransaction. replace (R. id. detail_container, detailfragment); If (ORI = configuration. orientation_portrait) {fragmenttransaction. addtobackstack (null);} fragmenttransaction. commit () ;}}) ;}/ ***** @ Param MSG */private void showtost (string MSG) {toast. maketext (getactivity (), MSG, toast. length_long ). show ();}}

4. Details fragment. In practice, we sometimes need to pass parameters from the list to the details page. Here we pass the display data of the list, which is also a listview

Package COM. example. fragmentdemo3; import Java. util. arraylist; import Java. util. list; import android. content. res. configuration; import android. OS. bundle; import android. support. v4.app. fragment; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; import android. widget. arrayadapter; import android. widget. linearlayout; import android. widget. listview;/*** details fragment * @ author mining **/public class fragmentdetail extends fragment {@ overridepublic view oncreateview (layoutinflater Inflater, viewgroup container, bundle identifier) {return Inflater. inflate (R. layout. fragment_detail_layout, container, false) ;}@ overridepublic void onactivitycreated (bundle savedinstancestate) {super. onactivitycreated (savedinstancestate); // obtain the bundle = getarguments (); string date = bundle. getstring ("Arg"); List <string> List = new arraylist <string> (); For (INT I = 0, Count = 20; I <count; I ++) {list. add (date);} linearlayout layout = (linearlayout) getactivity (). findviewbyid (R. id. detail_container); listview = (listview) getactivity (). findviewbyid (R. id. fragment_detail); listview. setadapter (New arrayadapter (getactivity (), android. r. layout. simple_list_item_1, list); configuration = getactivity (). getresources (). getconfiguration (); int Ori = configuration. orientation ;}}

5. Then there is activity. We dynamically load the list fragment.

Package COM. example. fragmentdemo3; import android. OS. bundle; import android. support. v4.app. fragment; import android. support. v4.app. fragmentactivity; import android. support. v4.app. fragmentmanager; import android. support. v4.app. fragmenttransaction; public class mainactivity extends fragmentactivity {@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); // fragmentfragment listfragment = new fragmentlist (); fragmentmanager = getsupportfragmentmanager (); fragmenttransaction = fragmentmanager to be displayed on the list page. begintransaction (); fragmenttransaction. add (R. id. list_container, listfragment); fragmenttransaction. commit ();}}

It's almost like that. It's 7 o'clock. It's off duty, haha! Upload code. If you are interested, you can download it.

Code download

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.