Android 动态添加Fragment的实例代码
程序员文章站
2024-03-07 08:40:56
1.fragment1布局及代码
布局
1.fragment1布局及代码
布局
<?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>
代码
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 oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_fragment1); } @override public void onchangefragment(int which) { if(which==1){ fragment fragment1=new firstfragment(); getsupportfragmentmanager() .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布局及代码
布局
<?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>
代码:
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(); getsupportfragmentmanager() .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代码及布局
布局:
<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="我是fragment1" android:layout_gravity="center_horizontal|bottom" /> </framelayout>
代码:
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 constructor } @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代码及布局
布局:
<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="我是fragment2" /> </framelayout>
代码:
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 constructor } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate the layout for this fragment return inflater.inflate(r.layout.fragment_first, container, false); } }
5.leftfragment布局及代码
布局:
<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:orientation="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="第一个fragment" android:id="@+id/bt1" /> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第二个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="隐藏" android:id="@+id/bt5" /> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="显示" android:id="@+id/bt6" /> </linearlayout>
代码:
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 myinterface myinterface ; public leftfragment() { } @override public void onattach(context context) { super.onattach(context); if (context instanceof myinterface) { myinterface= (myinterface) context; } else { throw new runtimeexception(context.tostring() + " must implement 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(), "点击了按钮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); } }
效果:
点击第一个按钮出现fragment1.
点击第二个按钮出现fragment2
点击第三个按钮出现fragment1.(方法不同)
点击第四个按钮出现fragment2.(方法不同)
点击隐藏,字条消失
点击显示,字条出现
以上所述是小编给大家介绍的android 动态添加fragment的实例代码,希望对大家有所帮助
上一篇: java 从int数组中获取最大数的方法