Two ways to use Android learning notes fragment

Source: Internet
Author: User

One, the first method:

(1) The first way to use fragment is to load a separate layout file using fragment: (That is, the way XML is implemented)

The structure is as follows:


Activity_main.xml is mainly to add two linear layouts in a linear layout

<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:orien tation= "Horizontal" tools:context= ". Mainactivity "> <linearlayout android:id=" @+id/linerlayout1 "android:layout_width=" 0DP "Android oid:layout_height= "Match_parent" android:layout_weight= "1" android:background= "#CCCCCC" Android:orien tation= "vertical" > <button android:id= "@+id/button1" android:layout_width= "Match_paren T "android:layout_height=" wrap_content "android:text=" display window "/> </LinearLayout> <li Nearlayout android:id= "@+id/linerlayout2" android:layout_width= "0DP" android:layout_height= "Match_par Ent "android:layout_weight=" 3 "android:background=" #CCFFDD "android:orientation=" vertical "> &L T;/lineaRlayout></linearlayout> 

Right.xml is a layout file that is loaded when the fragment is used: (because it is loaded in the interface, so no special requirements)

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <ratingbar        android:id= "@+id/ratingbar1" android:layout_width= "Wrap_        Content "        android:layout_height=" wrap_content "/>    <button        android:id=" @+id/button11        " Android:layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:text= "point I'll Try"/> </LinearLayout>

Myfragment.java is the class that loads fragment, to inherit the Fragment class: (To reload the three methods below the parent class)

Package Com.lc.tablet_fragment_addview;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.toast;public class MyFragment extends Fragment {public myfragment () {//TODO auto-generated constructor stub} @Overridepublic void OnCreate (Bundle Savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate);} @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {// The r.layout.right here is the interface of Idview view = inflater.inflate (r.layout.right, NULL); Button button = (button) View.findviewbyid (r.id.button11); Button.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View v) {toast.maketext (getactivity (), "Hello world!", Toast.length_long). Show ();}); return view;} @Overridepublic void OnPause () {//TODO auto-generated method Stubsuper. OnPause ();}} 

Mainactivity.java:

Package Com.lc.tablet_fragment_addview;import Android.app.activity;import Android.app.fragmentmanager;import Android.app.fragmenttransaction;import Android.os.bundle;import Android.view.menu;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;public class Mainactivity extends Activity {private Button button;private Fragmentmanager Fragmentmanager; Manage Private fragmenttransaction fragmenttransaction; Transaction @overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); button = (button) This.findviewbyid (r.id.button1); Fragmentmanager = Getfragmentmanager (); Button.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {fragmenttransaction = Fragmentmanager.begintransaction (); Myfragment myfragment = new Myfragment ();//The first parameter is the ID to which to place, and the second is the Fragmentfragmenttransaction.add to be placed ( R.id.linerlayout2, myfragment); Fragmenttransaction.commit ();}}); @OverridepubliC Boolean oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is Present.getmenui Nflater (). Inflate (R.menu.main, menu); return true;}}
Demo Effect: Displays the layout on the right when you click on the button on the gray screen:



The second method is similar to the one in the project structure: just use the fragment control directly in the layout file:
<relativelayout 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:paddingbottom= "@dimen/activity_vertical_margin"    android:paddingleft= "@dimen/activity_ Horizontal_margin "    android:paddingright=" @dimen/activity_horizontal_margin "    android:paddingtop=" @dimen /activity_vertical_margin "    tools:context=". Mainactivity ">    <fragment        android:id=" @+id/fragment1 "        android:name=" com.example.tablet_ Fragment_fragementmanager. Myfragment "        android:layout_width=" wrap_content "        android:layout_height=" Wrap_content "        android: Layout_alignparenttop= "true"        android:layout_centerhorizontal= "true"        android:layout_margintop= "37DP"/ ></RelativeLayout>

In the Myfragment.java file, you only need to find the layout file that the fragment accommodates, without doing business:
Package Com.example.tablet_fragment_fragementmanager;import Android.app.fragment;import Android.os.Bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public class MyFragment extends Fragment {public myfragment () {} @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate);} @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {/* * Just find the layout file here */view view = inflater.inflate (R.layout.text, null); return view;} @Overridepublic void Onresume () {Super.onresume ();}}


Mainactivity.java file: Business processing for fragment
Package Com.example.tablet_fragment_fragementmanager;import Android.app.activity;import Android.app.fragmentmanager;import Android.os.bundle;import Android.view.menu;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;import android.widget.toast;/* * Drag a fragment into the layout file , use the method below to find a specific fragment * do not need to use the BeginTransaction method */public class Mainactivity extends Activity {private myfragment Fragment;private fragmentmanager fragmentmanager;private button button, @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); FragmentManager = Getfragmentmanager ();//use Fragmentmanager to find fragment, use ID as unique identifier fragment = (myfragment) Fragmentmanager.findfragmentbyid (r.id.fragment1);//or use the method below to find fragment//fragment = (myfragment) Fragmentmanager.findfragmentbytag ("Fragment1");//Find the button Button1button = (button) Fragment.getview () in the fragment layout. Findviewbyid (R.id.button1); Button.setonclicklistener (NEW Onclicklistener () {@Overridepublic void OnClick (View v) {toast.maketext (mainactivity.this, "Hello world!", Toast.length_short). Show ();}}); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;}}



Two ways to use Android learning notes 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.