Framelayout and fragment handles Android application UI layout instance _android

Source: Internet
Author: User

The use of fragment and layout is generally the main activity to manage fragment interactively by using a frame-filled activity:

1. Due to the use of Getsupportfragmentmanager (), the extends of the main activity needs to be fragmentactivity:

  The public class Mainactivity extends fragmentactivity{...
  }

2. The layout (XML file) of the main activity establishes multiple frame and defines its android:id= "@+id/xxx"
3. In the operation function within the main activity, the framelayout are populated by the lookup ID:
Add a statement as follows

Copy Code code as follows:

Getsupportfragmentmanager (). BeginTransaction (). Add (R.id.fragment_container2,new Nullfrag ()). commit ();


Obtain support for Fragment management ()-> start trading ()-> Add (frame ID, Fragment). Delivery ();
The replacement statement is as follows
Getsupportfragmentmanager (). BeginTransaction (). Replace (R.id.fragment_container2,new Nullfrag ()). commit ();
Obtain support for Fragment management ()-> start trading ()-> substitution (frame ID, Fragment). Delivery ();
Cases:

Copy Code code as follows:

public void click_btn_flag02 (view view) {
Getsupportfragmentmanager (). BeginTransaction (). Replace (R.id.fragment_container2,new Midfrag ()). commit ();
}


Note: function parameters to use view

Here's a look at the example:


The left side of the effect chart is a list, and the right side is the details of the list item.
First look at the layout file (layout):

 <?xml version= "1.0" encoding= "Utf-8"?> <linearlayout "xmlns:android=" Schemas.android.com/apk/res/android "android:orientation=" horizontal "android:layout_width=" Match_parent "Android : layout_height= "Match_parent" > <fragment class= "com.fragment.main.TitlesFragment" android:id= "@+id/titles" a ndroid:layout_weight= "1" android:layout_width= "0px" android:layout_height= "match_parent"/> <FrameLayout Android:id= "@+id/details" android:layout_weight= "1" android:layout_width= "0px" android:layout_height= "Match_" Parent "android:background=" Android:attr/detailselementbackground "/> </LinearLayout> 

The fragment label and Framelayout label are used in the

        layout file. The Android fragment uses a method of embedding fragment in 2, which is used in this example, and is seen from the layout file with the fragment tag, which is a way to The framelayout tag will become the second carrier view to load fragment.
        See program Implementation (Com.fragment.main.TitlesFragment):

public class Titlesfragment extends listfragment {int mcurcheckposition = 0; 
 
 int mshowncheckposition =-1; 
               
  @Override public void onactivitycreated (Bundle savedinstancestate) {super.onactivitycreated (savedinstancestate); Setlistadapter (New arrayadapter<string> (getactivity), Android. R.layout.simple_list_item_activated_1, Shakespeare.titles)); 
   Populates the list with a static array if (Savedinstancestate!= null) {mcurcheckposition = Savedinstancestate.getint ("Curchoice", 0); 
  Mshowncheckposition = Savedinstancestate.getint ("Shownchoice",-1); 
   } getlistview (). Setchoicemode (Listview.choice_mode_single); 
 ShowDetails (mcurcheckposition); 
 
  @Override public void Onsaveinstancestate (Bundle outstate) {super.onsaveinstancestate (outstate); 
  Outstate.putint ("Curchoice", mcurcheckposition); 
 Outstate.putint ("Shownchoice", mshowncheckposition); @Override public void Onlistitemclick (ListView l, View v, int position, long ID) {showdetails (position); 
   /** * Display ListView Item Details/void showdetails (int index) {mcurcheckposition = index; 
 
   Getlistview (). setitemchecked (index, true); 
    if (mshowncheckposition!= mcurcheckposition) {detailsfragment df = detailsfragment.newinstance (index); 
    Fragmenttransaction ft = Getfragmentmanager (). BeginTransaction (); 
    Ft.replace (R.id.details, DF); 
    Ft.settransition (Fragmenttransaction.transit_fragment_fade); 
    Ft.commit (); 
   mshowncheckposition = index; 

 }  
 } 
 
}

Titlesfragment
Titlesfragment inherits from the fragment subclass Listfragment, uses a static array to populate the list, overrides the Onlistitemclick method, and ShowDetails the method to display the details of the ListView item.

Detailsfragment df = detailsfragment.newinstance (index);//Get details Fragment instance
fragmenttransaction ft = Getfragmentmanager (). BeginTransaction ()///Get Fragmenttransaction instance
ft.replace (r.id.details, DF); Use Detailsfragment instance
ft.settransition (fragmenttransaction.transit_fragment_fade);
Ft.commit ()/Submit

Take a look at Detailsfragment:

public class Detailsfragment extends Fragment {/** * Create a new instance of Detailsfragment, initialized to * sh  ow the text at ' index '. 
  */public static detailsfragment newinstance (int index) {detailsfragment f = new detailsfragment ();   
  Supply index input as an argument. 
  Bundle args = new Bundle (); 
  Args.putint ("index", index); 
  F.setarguments (args); 
 return F; 
  @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { 
  if (container = = null) {return null; 
  } scrollview scroller = new ScrollView (getactivity ()); 
 
  TextView Text = new TextView (getactivity ()); int padding = (int) typedvalue.applydimension (Typedvalue.complex_unit_dip, 4, Getactivity (). Getresources (). g 
  Etdisplaymetrics ()); 
  text.setpadding (padding, padding, padding, padding); 
  Scroller.addview (text); 
  Text.settext (Shakespeare.dialogue[getarguments (). GetInt ("index", 0)]); Return ScrolleR 

 } 
}

&NBSP
Detailsfragment uses the newinstance (int index) method to produce a detailsfragment instance and accepts an integer parameter, overloading the Oncreateview method to create the view.

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.