The first thing to introduce is our default layout.
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "Horizontal" > <FragmentAndroid:id= "@+id/fragment1"Android:name= "Com.example.chuang.frgone"Android:layout_width= "0DP"Android:layout_weight= "1"Android:layout_height= "Match_parent" /> <FragmentAndroid:id= "@+id/fragment2"Android:name= "Com.example.chuang.frgtwo"Android:layout_weight= "1"Android:layout_width= "0DP"Android:layout_height= "Match_parent" /></LinearLayout>
The attribute that needs attention is also the name attribute in fragment, which is key to the function of fragment automatically instantiating the corresponding class when the layout is loaded.
Now that you are loading two class files, you must load the corresponding layout,
Fragment_one Layout file:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <ListViewAndroid:id= "@+id/ls_show"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > </ListView></LinearLayout>
Fragment_two Layout file:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <TextViewAndroid:id= "@+id/tv_show"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "TextView" /> </LinearLayout>
And then all we need to do is bind the layout file to the class.
PackageCom.example.chuang;Importandroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup; Public classFrgtwoextendsFragment {@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {//TODO auto-generated Method Stub returninflater.inflate (R.layout.fragment_two, container); }}
Other bindings are similar, running effects like, here for you to see clearly, I added a background color in the Fragment_one,
To facilitate testing, we have defined the following fields
PackageCom.example.chuang;Importjava.util.ArrayList;Importandroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView; Public classFrgoneextendsFragment {PrivateListView ls=NULL; @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {view V=inflater.inflate (R.layout.fragment_one, container); LS=(ListView) V.findviewbyid (r.id.ls_show); Ls.setadapter (NewArrayadapter (Getactivity (), Android. R.layout.simple_list_item_1,getarray ())); returnv; } PrivateArraylist<string>GetArray () {ArrayList<String> al=NewArraylist<string>(); Al.add ("I am what I am."); Al.add ("Light my new."); Al.add ("Your heart, My Xin"); returnal; }}
As follows
So what we're going to do now is to click on the left and show the right side, that is, to realize the value of fragment.
The first method, which is the use of callbacks
PackageCom.example.chuang;Importjava.util.ArrayList;Importandroid.app.Activity;Importandroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.AdapterView;ImportAndroid.widget.AdapterView.OnItemClickListener;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView; Public classFrgoneextendsFragment {Private CallBack Back=null; PrivateListView ls=NULL; @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View V =inflater.inflate (R.layout.fragment_one, container); LS=(ListView) V.findviewbyid (r.id.ls_show); Ls.setadapter (NewArrayadapter (Getactivity (), Android. R.layout.simple_list_item_1,getarray ())); Ls.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?>Parent, view view,intPositionLongID) {if(back!=NULL) {back. Setteee (string.valueof (parent.getitematposition (position))); //Back . Setteee (GetString (GetId ())); } } }); returnv; } PrivateArraylist<string>GetArray () {ArrayList<String> al=NewArraylist<string>(); Al.add ("I am what I am."); Al.add ("Light my new."); Al.add ("Your heart, My Xin"); returnal; }Public interface callback{void setteee (String s);} @Override Public void Onattach (activity activity) {Super.onattach (activity); if (activity instanceof CallBack) {back= (CallBack) activity; } }}
And then the code for the main activity.
@Override publicvoid setteee (String s) { View vthis. Findviewbyid (R.id.fragment2); TextView t=(TextView) V.findviewbyid (r.id.tv_show); T.settext (s); }
as follows, so that we can implement the first method
The second method of
Using the second approach, we first need to look at the life cycle of fragment and activity
We can tell by the graph above that only when fragment is created, the activity notifies them to call the Onactivitycreated () method to tell
So we can get everything in the main activity here, what do you want to do? 0-0!
Put code on it.
PackageCom.example.chuang;Importjava.util.ArrayList;Importandroid.app.Activity;Importandroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.AdapterView;ImportAndroid.widget.AdapterView.OnItemClickListener;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView;ImportAndroid.widget.TextView; Public classFrgoneextendsFragment {PrivateCallBack back=NULL; PrivateListView ls=NULL;PrivateTextView tv=NULL; @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View V =inflater.inflate (R.layout.fragment_one, container); LS=(ListView) V.findviewbyid (r.id.ls_show); Ls.setadapter (NewArrayadapter (Getactivity (), Android. R.layout.simple_list_item_1,getarray ())); Ls.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?>Parent, view view,intPositionLongID) {if(back!=NULL) { if(tv!=NULL) Tv.settext (string.valueof (parent.getitematposition (position))); } } }); returnv; } @Override Public voidonactivitycreated (Bundle savedinstancestate) {Super. onactivitycreated (savedinstancestate); View v=getactivity (). Findviewbyid (R.id.fragment2); TV=( (TextView) V.findviewbyid (r.id.tv_show)); } PrivateArraylist<string>GetArray () {ArrayList<String> al=NewArraylist<string>(); Al.add ("I am what I am."); Al.add ("Light my new."); Al.add ("Your heart, My Xin"); returnal; }
The third approach, which is to manage two fragment in the activity, is also very simple, just after the activity's start life cycle can be
Transfer value between Android fragment