Fragment 动态切换碎片


//      FragmentManager fragmentManager = getSupportFragmentManager();
//      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//      fragmentTransaction.replace(R.id.right_layout,fragment);
//      fragmentTransaction.commit();

getSupportFragmentManager().beginTransaction().replace(R.id.right_layout,fragment).commit();
        //与上述注释句子等效

//getSupportFragmentManager()  获得FragmentManager 
//beginTransaction() 开启一个事务
//向容器内添加或者删除碎片  replace()
//使得添加碎片后点击返回 返回到上一个碎片 参数为null即可 addToBackStack(null)  
//commit()提交事务

getSupportFragmentManager().beginTransaction().replace(R.id.right_layout,fragment)
.addToBackStack(null) .commit();//使得添加碎片后点击返回 返回到上一个碎片


//右碎片类
public class RightFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //将右布局引入
        return inflater.inflate(R.layout.right_fragment,container,false);

    }
}

//右碎片布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#00ff00"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_gravity="center_vertical"
        android:background="#ffffff"
        android:text="右碎片"
        android:textSize="40sp"
        android:gravity="center"/>

</LinearLayout>

//左碎片类
public class LeftFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.left_fragment,container,false);//将左碎片布局引入

    }
}

//左碎片布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/leftButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="左碎片按钮"
        android:layout_gravity="center_horizontal"
        android:textSize="40sp"
        android:gravity="center"
        android:textColor="#000000"/>

</LinearLayout>

//第二个右碎片
public class AnotherLightFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.another_right_fragment,container,false);//将另一个碎片布局加入

    }
}

//右碎片布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#FFFF00"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:text="另一个碎片"
        android:textSize="40sp"
        android:gravity="center"
        android:textColor="#000000"/>


</LinearLayout>


//主活动
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button leftButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        leftButton = findViewById(R.id.leftButton);

        leftButton.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {


                replaceFragment(new AnotherLightFragment());//新的碎片取代旧碎片

            }

        });

        replaceFragment(new RightFragment());//初始化右边碎片

    }

    @Override
    public void onClick(View v) {

    }

    /*
      替换碎片布局
     */
    public void replaceFragment(Fragment fragment){

//        FragmentManager fragmentManager = getSupportFragmentManager();
//        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//        fragmentTransaction.replace(R.id.right_layout,fragment);
//        fragmentTransaction.commit();

        getSupportFragmentManager().beginTransaction().replace(R.id.right_layout,fragment).addToBackStack(null).commit();//与上述注释句子等效

    }

}

//主活动布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/left_fragment"
        android:name="com.example.tabletproject.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>


    <FrameLayout
        android:id="@+id/right_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />


</LinearLayout>

效果
1.切换前
在这里插入图片描述


2.切换后
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值