Static and dynamic addition of fragments (differentiate between phones and tablets)

Source: Internet
Author: User

I. static Fragmentation management

1. Create a New Left fragment layout:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
<button
Android:id= "@+id/button"
Android:layout_gravity= "Center_horizontal"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "button"/>
</LinearLayout>

2. Create a New Right fragment layout:

 <?xml version= "1.0" encoding= "Utf-8"? 
<linearlayout xmlns:android= "http://schemas.android.com/ Apk/res/android "
    android:orientation=" vertical "
    android:background=" #00ff00 "
    android:layout_width=" match_parent "
    android:layout_height=" Match_parent
    <textview
        android: Layout_width= "Wrap_content"
        android:layout_height= "Wrap_content"
        android:layout_gravity= "Center_horizontal"
         android:textsize= "20sp"
        android:text= " This was right Fragment "/>
</linearlayout>

3. New Leftfragment class:// Overridden the Oncreateview method

public class Leftfragment extends Fragment {
@Nullable
@Override
Public View Oncreateview (Layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle savedinstancestate) {
View view=inflater.inflate (r.layout. Left_fragment, Container,false);
return view;
}
}

4. New Rightfragment class:

public class Rightfragment extends Fragment {
@Nullable
@Override
Public View Oncreateview (Layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle savedinstancestate) {
View view=inflater.inflate (r.layout. Right_fragment, Container,false);
return view;
}
}

5. Modify the Activity_main.xml code:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
xmlns:app= "Http://schemas.android.com/apk/res-auto"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Horizontal" >
<fragment
Android:id= "@+id/left_fragment"
Android:name= "Com.example.fragmenttest.LeftFragment"
Android:layout_width= "0DP"
android:layout_height= "Match_parent"
android:layout_weight= "1"
></fragment>
<fragment
Android:id= "@+id/right_fragment"
Android:name= "Com.example.fragmenttest.RightFragment"
Android:layout_width= "0DP"
android:layout_height= "Match_parent"
android:layout_weight= "1"
></fragment>
</LinearLayout>
6. Running results

Two. adding fragments dynamically

Target: Add click function to the button on the left, dynamically change the background color of the right to yellow, the content is the this is another-fragment.

1. Create a new Right_fragment.xml file with the following code:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:background= "#ffff00" >
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_gravity= "Center_horizontal"
Android:textsize= "20SP"
Android:text= "This was another right fragment"/>
</LinearLayout>

2. Create a new Java class for anotherrightfragment, overriding the Oncreateview method. Code:

public class Anotherrightfragment extends fragment{

@Nullable
@Override
Public View Oncreateview (Layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle savedinstancestate) {
View view=inflater.inflate (r.layout. Another_right_fragment, Container,false);
return view;
}
}

3. Change the code in the Activity_main.xml.

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
xmlns:app= "Http://schemas.android.com/apk/res-auto"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Horizontal" >
<fragment
Android:id= "@+id/left_fragment"
Android:name= "Com.example.fragmenttest.LeftFragment"
Android:layout_width= "0DP"
android:layout_height= "Match_parent"
android:layout_weight= "1"
></fragment>
<framelayout
Android:id= "@+id/right_layout"
Android:layout_width= "0DP"
android:layout_height= "Match_parent"
android:layout_weight= "1" ></FrameLayout>
</LinearLayout>

4. Modify the code in the mainactivity:

public class Mainactivity extends appcompatactivity implements view.onclicklistener{
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.Activity_main);
Button button= (button) Findviewbyid (r.id.Button);
Button.setonclicklistener (this);
Replacefragment (New Rightfragment ());
}
@Override
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id.Button:
Replacefragment (New Anotherrightfragment ());
Break
Default
Break
}
}
private void Replacefragment (Fragment Fragment) {
Fragmentmanager Fragmentmanager=getsupportfragmentmanager ();
Fragmenttransaction transaction=fragmentmanager.begintransaction ();
Transaction.replace (r.id.Right_layout, fragment);
Transaction.commit ();
}
}

5. Dynamic Fragmentation Management results:

Static and dynamic addition of fragments (differentiate between phones and tablets)

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.