Android Growth Diary-fragment

Source: Internet
Author: User

(a) Android introduced the concept of fragment in 3.0, the main purpose of which is to support more dynamic and flexible UI design on large-screen devices such as tablets. Tablets have more screens than phones, more space for more UI components, and more interaction between these components.

(b) Fragment should be a modular and reusable component in your application because fragment defines his own layout and defines his own behavior by using his own life cycle callback method, you can include fragment in multiple activities

(iii) "Summary of Fragment Knowledge"

(1) fragment can be formed as part of the activity interface

(2) Multiple fragment can be in one activity simultaneously, and a fragment can be run in multiple activity

(3) During activity operation, you can add, remove, and replace fragment

(4) fragment can respond to their own input events, and have their own life cycle, their life cycle will be affected by the life cycle of the host activity

-------------------------------------------------------------------

(iv) Loading mode of fragment

(1) Static loading

To declare Fragment in the activity's layout file, it is important to note that the Android:name property in <Fragment> specifies the Fragment class that is instantiated in layout

The method of--à marking fragment

The Android:id property provides a unique ID

The Android:tag property provides a unique string

Eg:

Implement the name of the <fragment>,<fragment> in the layout file as a class, in the class to convert another layout containing the control into a View object, the View object-à the previous action in the layout file (keywords: Static loading )

1. Create a main in layout and store the Fragment.xml in main

(Ps:fragment contains Id,width,height,name)

2. Create a fragment_kongjian.xml in layout, containing components (Textview,button)

3. Create a Fragment.java and create a method:

Public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate)

4. Convert layout layouts file to view object

View view = Inflater.inflate (R.layout.fragment_kongjian, container, false)

Ps (resource:fragment needs to load the layout file

Root: Loads the layout of the parent viewgroup;

Attacttoroot:false, do not return parent ViewGroup)

5. Initialize the space in the Fragment_kongjian.xml

TextView text= (TextView) View.findviewbyid (R.id.text);

Button button= (button) View.findviewbyid (R.id.button)

6. Operation slightly

7. Create intent in Mainactivity.java, and when the button is clicked, jump to Secondactivity.java,secondactivity.java with a OnCreate method to open the main layout->1. Main.xm

8. Specific location of the Name:Fragment.java for the layout of the frame in Main.xml

------------------------------------------------------------------

(2) Dynamic loading

Write code to add fragment to an activity layout

Add (): Adds a fragment (specifies the fragment to be added and the inserted view)

Similar to this is the Remove ()/replace ()

* Handling Fragment transactions

Each set of changes submitted to the activity is referred to as a transaction, depending on the user's interaction, adding, removing, replacing, and performing other operations on the fragment.

Fragmentmanager Fragmentmanager==getfragmentmanager ();

Fragmenttransaction begintransaction= fragmentmanager.begintransaction ();

Eg:

Implementation steps:

1. Fragment fragment=new Fragment ();

This fragment is a Fragment.java that contains the view object.

2. PS: Operate in a transactional manner

Fragmentmanager Fragmentmanager==getfragmentmanager ();

Fragmenttransaction begintransaction= fragmentmanager.begintransaction ();

Begintransaction.add ("R.id.frame" [S1], framemanager[s2]);

BEGINTRANSACTION.ADDTOBACKSTACK[S3] (NULL);

Begintransaction.commit ();//Commit a transaction

-----------------------------------------------------------------------

PS: Each transaction performs a set of changes at the same time, you can set all the changes you want to perform in a transaction, including add (), remove (), replace (), and then commit to the activity, you must call the Commit () method-à the same as the transaction submission method of the database

If the user is allowed to return to the previous fragment state by pressing the back key, the Addtobackstack () method can be added before calling commit ()

-----------------------------------------------------------------------

[S1] a LinearLayout

[S2] contains the control's view object for the current period of the Fragmentmanager.java

[S3] Add physical return button to return to the previous interface

Android Growth Diary-fragment

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.