Description First: We created the project in the previous way, if you are using the latest Adt,minimum Android SDK with the android2.* or 1.* selected, a compatible project is created by default-- APPCOMPAT_V7, this item still cannot delete, delete will error.
Description Second: After the project has been created, you will find the default two files in the layout folder (layouts): Adtivity and Fragment files:
1, Fragment Overview:
Fragment meaning for fragments, fragments, plainly is the module, said the module, I do not have to say more.
Characteristics:
Fragment is always used in "embedding" activity as an interface component of activity;
An activity can contain multiple fragment, and a fragment can also be reused by multiple activity;
Fragment can respond to their own events and also have their own life cycle.
First, let's look at what we want to achieve:
2. fragment_main.xml File:
The contents are as follows:
< 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 "android:orientation=" vertical " tools:context= "Com.javen.xresource.activity.fragment.MainFragment" > <textview android:id= "@+id/ftxv" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/fragment" /> <button android:id= "@+id/btn" android:layout_width= "Wrap_content" android:layout_height = "Wrap_content" android:text= "@string/change_fragment"/> <button android:id= "@+id/btn2" and Roid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/change_activity" /></linearlayout>
Two buttons and a text display are defined in the fragment, and a text display is defined in the activity, and clicking the fragment different buttons controls the text display.
Activity_main.xml content is as follows:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:id=" @+id/container " android:layout_width=" Match_parent " Android : layout_height= "match_parent" android:orientation= "vertical" tools:context= " Com.javen.xresource.activity.MainActivity " tools:ignore=" Mergerootframe "> <textview android: Id= "@+id/atxv" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android: text= "@string/activity"/> <!--XML embedded in activity fragment--- <fragment android:name= " Com.javen.xresource.activity.fragment.MainFragment " android:id=" @+id/mft " android:layout_width=" wrap _content " android:layout_height=" wrap_content "/> </LinearLayout>
3. Create Mainfragment.java class:
Package Com.javen.xresource.activity.fragment;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.button;import Android.widget.textview;import Com.javen.xresource.activity.r;public class Mainfragment extends Fragment {private TextView fragmenttextview;private Button button;public mainfragment () {} @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View Rootview = inflater.inflate (R.layout.fragment_main, container,false);//Use Fragment_ Main.xml Initialize Fragmentbutton = (Button) Rootview.findviewbyid (R.ID.BTN);//Get fragment in Buttonfragmenttextview = ( TextView) Rootview.findviewbyid (R.ID.FTXV);//Get Textviewbutton.setonclicklistener in Fragment (New Onclicklistener () {//Set the Listener event for the button @overridepublic void OnClick (View v) {fragmenttextview.settext ("I was fragment clicked ...");}); return Rootview;}}
at this time fragment has been done ...
4, Mainactivity.java
The code is as follows:
Package Com.javen.xresource.activity;import Android.app.activity;import Android.app.fragment;import Android.os.bundle;import Android.view.menu;import Android.view.menuitem;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.textview;public Class Mainactivity extends Activity {private Button button;private TextView activitytextview; @Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);/*if (savedinstancestate = = null) { Getfragmentmanager (). BeginTransaction (). Add (R.id.container, New Mainfragment ()). commit ();// Java code embeds fragment in activity as in the previous XML}*/activitytextview = (TextView) Findviewbyid (R.ID.ATXV);// Get textviewfragment fragment = Getfragmentmanager () in activity. Findfragmentbyid (R.ID.MFT);// Get Fragmentbutton = (Button) Fragment.getview () in activity. Findviewbyid (R.ID.BTN2);// Gets the Button2button.setonclicklistener in fragment (new Onclicklistener () {@Overridepublic void OnClick (View v) {activitytextview.settext ("I was clicked by activity ...");}); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem item) {int id = item.getitemid (); if (id = = r.id.action_settings) { return true;} return super.onoptionsitemselected (item);}}
Run the project ...
6, the question came: Button2 change activity in TextView operation can put in fragment? --Extend: Can fragment communicate with the activity?
After the derivative question, the communication is certainly possible.
As for the operation of the TextView is placed in the fragment ... I have tried, using:
Getactivity (). Findviewbyid (R.ID.ATXV);
is not able to get to the view element in the activity, this statement returns NULL. SoButton2 's operation still has to be put in the activity.
As for the reason? You are welcome to comment to discuss this issue ...