Android Dynamic Add fragment instance code _android

Source: Internet
Author: User
Tags commit

1.fragment1 Layout and code

Layout

<?xml version= "1.0" encoding= "Utf-8"?> <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" tools:context= ". Fragment1activity "> <fragment android:layout_width=" match_parent "android:layout_height=" 100DP "Android:name = "Com.example.administrator.jreduch06.fragment.TopFragment" android:id= "@+id/top_fragment" Android:layout_ Alignparenttop= "true" android:layout_alignparentend= "true" > </fragment> <fragment android:layout_width = "Match_parent" android:layout_height= "300DP" android:id= "@+id/leftfragment" Android:name= " Com.example.administrator.jreduch06.fragment.LeftFragment "android:layout_below=" @+id/top_fragment "Android: Layout_alignparentstart= "true" > </fragment> <framelayout android:layout_width= "Match_parent" Android: layout_height= "Match_parent" android:id= "@+id/fl" android:layout_alignparentstart= "true" Android:layout_below= "@+id/leftfragment" > </FrameLayout> </RelativeLayout> 

Code

 package com.example.administrator.jreduch06; import android.os.Bundle; import
android.support.v7.app.AppCompatActivity;
Import android.support.v4.app.Fragment;
Import com.example.administrator.jreduch06.fragment.FirstFragment;
Import com.example.administrator.jreduch06.fragment.LeftFragment;
Import com.example.administrator.jreduch06.fragment.SecondFragment; public class Fragment1activity extends Appcompatactivity implements Leftfragment.myinterface {@Override protected void O Ncreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_
FRAGMENT1); @Override public void onchangefragment (int which) {if (which==1) {Fragment fragment1=new firstfragment (); Getsupportfra
Gmentmanager (). BeginTransaction (). replace (R.ID.FL, fragment1). commit (); }else if (which==2) {Fragment fragment2=new secondfragment () Getsupportfragmentmanager (). BeginTransaction (). Replace
(R.id.fl,fragment2). commit (); }
}
}

2.fragment2 Layout and code

Layout

<?xml version= "1.0" encoding= "Utf-8"?> <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"
tools:context= "com.example.administrator.jreduch06.Fragment2Activity" >
<fragment
android:layout_width= "match_parent"
android:layout_height= "Wrap_content"
Android:id= "@+id/one_fragment"
android:name= " Com.example.administrator.jreduch06.fragmentcallback.OneFragment "
>
</fragment>
< Framelayout
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:id = "@+id/fl2"
android:layout_below= "@+id/linearlatout"
>
</FrameLayout>
</ Relativelayout>

Code:

Package com.example.administrator.jreduch06;
Import android.support.v4.app.Fragment;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import com.example.administrator.jreduch06.fragment.FirstFragment;
Import com.example.administrator.jreduch06.fragment.SecondFragment;
Import com.example.administrator.jreduch06.fragmentcallback.OneFragment; public class Fragment2activity extends appcompatactivity implements onefragment.onfragmentinteractionlistener{@ Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.LAYOUT.ACTIVITY_FRAGMENT2); @Override public void changefragment (int which) {if (which==1) {Fragment fragment1=new firstfragment (); getsupportfragm
Entmanager (). BeginTransaction (). replace (R.ID.FL2, fragment1). commit (); }else if (which==2) {Fragment fragment2=new secondfragment () Getsupportfragmentmanager (). BeginTransaction (). Replace
(R.id.fl2,fragment2). commit (); }
}
}

3.FirstFragment Code and layout

Layout:

<framelayout 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 "
tools:context= "Com.example.administrator.jreduch06.fragment.FirstFragment" > 
<textview
Android:layout_width= "Match_parent"
android:layout_height= "match_parent"
android:gravity= "center"
android:textsize= "30sp"
android:id= "@+id/tv"
android:text= "I am Fragment1"
android:layout_ gravity= "Center_horizontal|bottom"/>
</FrameLayout>

Code:

Package com.example.administrator.jreduch06.fragment;
Import Android.os.Bundle;
Import android.support.v4.app.Fragment;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import COM.EXAMPLE.ADMINISTRATOR.JREDUCH06.R;
/**
* A simple {@link Fragment} subclass.
*
/public class Secondfragment extends Fragment {public
secondfragment () {
//Required Empty Public const Ructor
}
@Override public
View Oncreateview (layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate) {
//inflate the layout for this fragment return
inflater.inflate ( R.layout.fragment_second, container, false);
}

4.SecondFragment Code and layout

Layout:

<framelayout 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 "
tools:context= "Com.example.administrator.jreduch06.fragment.SecondFragment" >
<textview
Android:layout_width= "Match_parent"
android:layout_height= "match_parent"
android:gravity= "center"
Android:textsize= "30SP"
android:text= "I am Fragment2"/>
</FrameLayout>

Code:

Package com.example.administrator.jreduch06.fragment;
Import Android.os.Bundle;
Import android.support.v4.app.Fragment;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import COM.EXAMPLE.ADMINISTRATOR.JREDUCH06.R;
/**
* A simple {@link Fragment} subclass.
*
/public class Firstfragment extends Fragment {public
secondfragment () {
//Required empty public constr Uctor
}
@Override public
View Oncreateview (layoutinflater inflater, ViewGroup container,
Bundle Savedinstancestate) {
//inflate the layout for this fragment return
inflater.inflate (r.layout.fragment_ I, container, false);
}

5.LeftFragment Layout and code

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:o" rientation= "vertical" android:background= "#bece0d" tools:context= "
Com.example.administrator.jreduch06.fragment.LeftFragment "> <button android:layout_width=" match_parent " android:layout_height= "Wrap_content" android:text= "First Fragment" android:id= "@+id/bt1"/> <button android:
Layout_width= "Match_parent" android:layout_height= "Wrap_content" android:text= "second Fragment" android:id= "@+id/bt2"
/> <button android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "Callback1"
Android:id= "@+id/bt3"/> <button android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:text= "Callback2" android:id= "@+id/bt4"/> <button android:layout_width= "Match_parent" Android:layout_ height= "Wrap_content" android:text= "hidden" android:id="@+id/bt5"/> <button android:layout_width= match_parent "android:layout_height=" Wrap_content "android:text=" Show "android:id=" @+id/bt6 "/> </LinearLayout>

Code:

Package com.example.administrator.jreduch06.fragment;
Import Android.content.Context;
Import Android.os.Bundle;
Import android.support.v4.app.Fragment;
Import Android.support.v4.app.FragmentManager;
Import android.support.v4.app.FragmentTransaction;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.Button;
Import Android.widget.Toast;
Import COM.EXAMPLE.ADMINISTRATOR.JREDUCH06.R;
/** * A Simple {@link Fragment} subclass. * * public class Leftfragment extends Fragment {private Fragment fragment1; private Fragment fragment2; private MYINTERFAC
e MyInterface; Public leftfragment () {} @Override the public void Onattach (context) {Super.onattach (context); if (context Instanceo F MyInterface) {myinterface= (MyInterface) context;} else {throw new RuntimeException (context.tostring () + Must imple
ment Onfragmentinteractionlistener "); @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, BundLe Savedinstancestate) {View view=inflater.inflate (R.layout.fragment_left, container, false);
Button bt1= (button) View.findviewbyid (R.ID.BT1);
Button bt2= (button) View.findviewbyid (R.ID.BT2);
Button bt3= (button) View.findviewbyid (R.ID.BT3);
Button bt4= (button) View.findviewbyid (R.ID.BT4);
Button bt5= (button) View.findviewbyid (R.ID.BT5);
Button bt6= (button) View.findviewbyid (R.ID.BT6); Bt1.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {toast.maketext (GetContext ()
, "Click the button 1", Toast.length_short). Show ();
Fragment1=new firstfragment ();
Fragmentmanager Fm=getfragmentmanager ();
Fragmenttransaction fr=fm.begintransaction ();
Fr.replace (R.ID.FL,FRAGMENT1);
Fr.commit ();
}
}); Bt2.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {fragment2 = new secondfragment
();
Fragmentmanager fm = Getfragmentmanager ();
Fragmenttransaction fr = Fm.begintransaction ();
Fr.replace (R.ID.FL, Fragment2);
Fr.commit ();
}
}); Bt3.setoncLicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {myinterface.onchangefragment (1);}});
Bt4.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {
Myinterface.onchangefragment (2);
}
}); Bt5.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {if fragment1!=null& &!fragment1.ishidden ()) {Getfragmentmanager (). BeginTransaction (). Hide (Fragment1). commit ();} if (fragment2!=
null&&!fragment2.ishidden ()) {Getfragmentmanager (). BeginTransaction (). Hide (Fragment2). commit ();
}); Bt6.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {if fragment1!=null& &fragment1.ishidden ()) {Getfragmentmanager (). BeginTransaction (). Show (Fragment1). commit ();} if (fragment2!=
null&& Fragment2.ishidden ()) {Getfragmentmanager (). BeginTransaction (). Hide (Fragment2). commit ();
});
return view; } public interface MyInterface {void onchangefragment(int which); }
}

Effect:

Click on the first button to appear Fragment1.

Click on the second button to appear Fragment2

Click on the third button to appear Fragment1. (Different method)

Click on the Fourth button to appear Fragment2. (Different method)

Click Hide, note disappears

Click Show, Note appears

The above is a small set to introduce the Android dynamic add fragment instance code, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.