Fragment的静态添加和动态添加

静态添加:

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值