Fragment的静态添加和动态添加

来源:互联网 发布:it 网络 编辑:程序博客网 时间:2024/06/06 02:23

静态添加:

activity_main(静态添加fragment时,activity_main中的fragment标签,必须加name属性(包名 + 类名))

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:orientation="vertical"    android:layout_height="match_parent">    <fragment        android:id="@+id/frg_a"        android:name="com.example.staticfragment.FragmentA"        android:layout_width="match_parent"        android:layout_height="200dp"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="FragmentA传值"        android:onClick="sendData"/></LinearLayout>



MainActivity

/** * 如果activity中要使用fragment,必须使activity继承FragmentActivity * * -----------静态使用fragment步骤:---------- * 1.创建一个fragmentsupport.v4 * 2.重写fragment中的onCreateView()方法 * 3.创建一个layout并使用fragment加载器生成view并返回 * 4.activity的布局文件中添加fragment标签,加入name标签(包名+类名) * */public class MainActivity extends AppCompatActivity {    private FragmentA fragmentA;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //fragment管理器        FragmentManager manager = getSupportFragmentManager();        //获取FragmentA的对象        fragmentA = (FragmentA) manager.findFragmentById(R.id.frg_a);    }    public void sendData(View view) {        fragmentA.print("哈哈");    }}



FragmentA

/** * 如果activity中要使用fragment,必须使activity继承FragmentActivity * * -----------静态使用fragment步骤:---------- * 1.创建一个fragmentsupport.v4 * 2.重写fragment中的onCreateView()方法 * 3.创建一个layout并使用fragment加载器生成view并返回 * 4.activity的布局文件中添加fragment标签,加入name标签(包名+类名) * */public class MainActivity extends AppCompatActivity {    private FragmentA fragmentA;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //fragment管理器        FragmentManager manager = getSupportFragmentManager();        //获取FragmentA的对象        fragmentA = (FragmentA) manager.findFragmentById(R.id.frg_a);    }    public void sendData(View view) {        fragmentA.print("哈哈");    }}


fragment_a

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:orientation="vertical"    android:layout_height="match_parent">    <fragment        android:id="@+id/frg_a"        android:name="com.example.staticfragment.FragmentA"        android:layout_width="match_parent"        android:layout_height="200dp"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="FragmentA传值"        android:onClick="sendData"/></LinearLayout>


动态添加:



activity_main

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:orientation="vertical"    android:layout_height="match_parent">    <RelativeLayout        android:id="@+id/fragment_container"        android:layout_width="match_parent"        android:layout_height="200dp"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="切换Fragment"        android:onClick="onClick"/></LinearLayout>


MainActivity

public class MainActivity extends AppCompatActivity {    private FragmentA mFragmentA;    private boolean isFragmentB = true;    private FragmentB mFragmentB;    private boolean isFrist = true;    private RelativeLayout mFragmentContatiner;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mFragmentContatiner = (RelativeLayout) findViewById(R.id.fragment_container);        mFragmentA = new FragmentA();        mFragmentB = new FragmentB();        if (isFrist) {            //通过开始事务来获取事务对象            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();            //通过add方法将容器和fragment进行绑定            transaction.add(R.id.fragment_container, mFragmentA);            //提交事务(没有调用,前面代码无效)            transaction.commit();            isFrist = false;        }    }    public void onClick(View view) {        if (!isFrist) {            if (isFragmentB) {                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();                transaction.remove(mFragmentA);                transaction.add(R.id.fragment_container, mFragmentB);                transaction.commit();                isFragmentB = false;            } else {                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();                transaction.remove(mFragmentB);                transaction.add(R.id.fragment_container, mFragmentA);                transaction.commit();                isFragmentB = true;            }        }    }}


FragmentA

public class FragmentA extends Fragment {        @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment_a, container, false);        return view;    }    //如果需要从Activity传值到Fragment需要重写该方法    @Override    public void setArguments(Bundle args) {        super.setArguments(args);    }}


fragment_a

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:background="#0f0"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="fragmentA"/></LinearLayout>



FragmentB

public class FragmentB extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment_b, container, false);        return view;    }}


fragment_b

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:background="#f00"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="fragmentB"/></LinearLayout>

0 0