Android 动态添加Fragment的实例代码

Android 动态添加Fragment的实例代码,第1张

概述1.fragment1布局及代码布局<?xmlversion=\"1.0\"encoding=\"utf-8\"?><RelativeLayoutxmlns:android=\"http://schemas.android.com/apk/res/android\"

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"><fragmentandroID: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><fragmentandroID: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><FrameLayoutandroID: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 {@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentVIEw(R.layout.activity_fragment1);}@OverrIDepublic 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"><fragmentandroID:layout_wIDth="match_parent"androID:layout_height="wrap_content"androID:ID="@+ID/one_fragment"androID:name="com.example.administrator.jreduch06.fragmentcallback.OneFragment"></fragment><FrameLayoutandroID: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 AppCompatActivityimplements OneFragment.OnFragmentInteractionListener{@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentVIEw(R.layout.activity_fragment2);}@OverrIDepublic 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"> <TextVIEwandroID: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}@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentreturn 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"><TextVIEwandroID: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}@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater,Bundle savedInstanceState) {// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_first,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"><buttonandroID:layout_wIDth="match_parent"androID:layout_height="wrap_content"androID:text="第一个Fragment"androID:ID="@+ID/bt1"/><buttonandroID:layout_wIDth="match_parent"androID:layout_height="wrap_content"androID:text="第二个Fragment"androID:ID="@+ID/bt2"/><buttonandroID:layout_wIDth="match_parent"androID:layout_height="wrap_content"androID:text="callback1"androID:ID="@+ID/bt3"/><buttonandroID:layout_wIDth="match_parent"androID:layout_height="wrap_content"androID:text="callback2"androID:ID="@+ID/bt4"/><buttonandroID:layout_wIDth="match_parent"androID:layout_height="wrap_content"androID:text="隐藏"androID:ID="@+ID/bt5"/><buttonandroID: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() {}@OverrIDepublic voID onAttach(Context context) {super.onAttach(context);if (context instanceof Myinterface) {myinterface= (Myinterface) context;} else {throw new RuntimeException(context.toString()+ " must implement OnFragmentInteractionListener");}}@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater,Bundle savedInstanceState) {VIEw vIEw=inflater.inflate(R.layout.fragment_left,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() {@OverrIDepublic 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() {@OverrIDepublic 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() {@OverrIDepublic voID onClick(VIEw v) {myinterface.onchangeFragment(1);}});bt4.setonClickListener(new VIEw.OnClickListener() {@OverrIDepublic voID onClick(VIEw v) {myinterface.onchangeFragment(2);}});bt5.setonClickListener(new VIEw.OnClickListener() {@OverrIDepublic 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() {@OverrIDepublic 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的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android 动态添加Fragment的实例代码全部内容,希望文章能够帮你解决Android 动态添加Fragment的实例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1148652.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存