// 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.切换后