一、Framgent的简单用法
1.创建两个layout布局文件
java
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LeftFragment">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="178dp"
android:layout_gravity="center_horizontal"
android:text="button"
android:textSize="20sp" />
</LinearLayout>
2.引入这个两个布局文件
在主活动布局文件中用<fragment>标签引入这个两个布局文件
java
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/left_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.example.myapplication.LeftFragment" />
<fragment
android:id="@+id/right_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.example.myapplication.RightFragment" />
</LinearLayout>
3.创建对应的两个java文件
继承Fragment类,导入的是
import androidx.fragment.app.Fragment;
java
public class LeftFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.left_fragment,container,false);
return view;
}
}
二、动态添加Fragment
1.修改主活动布局文件
右侧从<fragment>标签改为<FrameLayout>
java
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/left_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.example.myapplication.LeftFragment" />
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
2.修改主活动文件
1. 获取 FragmentManager
FragmentManager fragmentManager = getSupportFragmentManager();
-
作用:获取 Fragment 的管理器
-
说明:
-
getSupportFragmentManager()来自AppCompatActivity -
负责管理 Activity 中的 Fragment(添加、移除、替换等操作)
-
使用 Support 库版本以保证向后兼容
-
2. 开始事务
FragmentTransaction transaction = fragmentManager.beginTransaction();
-
作用:开始一个 Fragment 事务
-
说明:
-
所有 Fragment 操作(添加、移除、替换等)必须在事务中执行
-
事务确保多个操作要么全部成功,要么全部失败
-
类似于数据库事务的概念
-
3. 执行替换操作
transaction.replace(R.id.right_layout, fragment);
-
作用:用新的 Fragment 替换指定容器中的当前 Fragment
-
参数说明:
-
R.id.right_layout:容器视图的 ID(通常是一个 FrameLayout) -
fragment:要替换进去的新 Fragment 实例
-
-
替换过程:
-
移除容器中当前所有的 Fragment
-
添加新的 Fragment 到容器中
-
4. 提交事务
transaction.commit();
-
作用:提交事务,使替换操作生效
-
说明:
-
事务只有在调用
commit()后才会执行 -
提交后,Fragment 的替换会立即安排执行(可能在主线程空闲时)
-
java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
replaceFragment(new RightFragment());
}
@Override
public void onClick(View v) {
if(R.id.button == v.getId()){
replaceFragment(new AnotherRightFragment());
}else{
return;
}
}
private void replaceFragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout,fragment);
transaction.commit();
}
}
三、模拟返回栈效果
现在运行程序之后,点击按钮,然后点击导航返回会直接退出程序,可以添加一行代码在点击导航返回的时候返回上一个碎片
在事务中添加:
transaction.addToBackStack(null);