Android Add fragmentation dynamically

Source: Internet
Author: User

    • Creation of fragments

To use fragmentation to create a fragment first, creating a fragment is simple.

    1. Create a new fragment layout, Fragment.xml
<?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">    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "This is fragment 1"/></LinearLayout>

2. Create a new class Fragment1.java, inherit from fragment, note that fragment has two different packages, it is recommended to use SUPPORT-V4, compatibility is better, and the other Android 4.2 will crash. There are various operations that can be performed in the fragment, just as an activity is manipulated.

 Public class extends Fragment {    @Nullable    @Override    public  View oncreateview (Layoutinflater inflater, @Nullable viewgroup container, Bundle savedinstancestate) {        view view=inflater.inflate (r.layout.fragment_ Questions1,container,false);        LOG.D ("QuestionMain1", "Fragment 1 Loading");         return view;    }}

The communication between fragments and activities. Although fragments are embedded in the activity, the relationship between them is not obvious.

1. The method of invoking fragmentation in the activity. Fragmentmanagert provides a method similar to Finviewbyid () to get an instance of fragmentation from a layout file. If it is dynamically loaded it is simple to load and you will have an instance of that fragment.

2. Invoke the active method in the fragment. The active instance that is bound to the current fragment can be obtained through the getactivity () method.

    • Binding of fragments
    1. Static bindings

Adding a fragment label to the active layout is relatively simple and does not elaborate. Android:name= "", the label is the class of the fragment, and note that the full name of the path is included.

<?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">    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "This is fragment 3"/>    <FragmentAndroid:id= "@+id/fragment1"Android:name= "Com.example.fragment1"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"        /></LinearLayout>

2. Dynamic binding, this is the strength of the fragment, when the program is dynamically added to the fragment, depending on the situation to dynamically add fragmentation, you can customize the program interface more diverse (more for adaptive mobile phones and tablet applications)

The following code to click the button. There are three fragments that dynamically toggle the displayed fragments in one activity by clicking on the event.

 Packagecom.xiaobu.xiaoyan1.question;ImportAndroid.os.Bundle;Importandroid.support.v4.app.Fragment;ImportAndroid.support.v4.app.FragmentManager;Importandroid.support.v4.app.FragmentTransaction;ImportAndroid.view.View;ImportAndroid.widget.TextView;ImportCOM.XIAOBU.XIAOYAN1.R;Importcom.xiaobu.xiaoyan1.base.BaseActivity; Public classQuestionsmainextendsBaseactivityImplementstextview.onclicklistener{PrivateTextView fragment1; PrivateTextView Fragment2; PrivateTextView Fragment3; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_question_main);    Initview (); }    Private voidInitview () {((TextView) Findviewbyid (R.id.question_text)). SetTextColor (Getresources (). GetColor (        r.color.colortextchecked)); Fragment1=(TextView) Findviewbyid (R.id.quiz_text_view); Fragment2=(TextView) Findviewbyid (R.id.answer_text_view); Fragment3=(TextView) Findviewbyid (R.id.chosen_text_view); Fragment1.setonclicklistener ( This); Fragment2.setonclicklistener ( This); Fragment3.setonclicklistener ( This); Changefragment (NewQuestionsMain1 ());    Checkedchange (FRAGMENT1); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.quiz_text_view:changefragment (NewQuestionsMain1 ());  Break;  CaseR.id.answer_text_view:changefragment (NewQuestionsMain2 ());  Break;  CaseR.id.chosen_text_view:changefragment (NewQuestionsMain3 ());  Break; default:                 Break; }    }    Private voidchangefragment (Fragment Fragment) {Fragmentmanager Fragmentmanager=Getsupportfragmentmanager (); Fragmenttransaction Transaction=fragmentmanager.begintransaction ();        Transaction.replace (r.id.main_view,fragment);//The first parameter represents the ID of the container, and the second parameter is a fragment instance.    Transaction.commit (); }}

   

Android Add fragmentation dynamically

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.