首先是静态添加fragment
创建相应的fragment文件继承Fragment,
查阅源码可以看出通过inflate方法来将layoutresource(布局资源)放入container(容器)中,
public View inflate(int resource, @Nullable ViewGroup root)
返回view即可将fragment中的内容通过fragment在Main_Activity文件中进行匹配设置,进而将fragment中的内容显示出来。
//继承自Fragment
public class Fragment extends androidx.fragment.app.Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment,container);//将layoutresource(布局资源)放入container(容器)中
return view;
}
}
在上述代码使用中,会出现以下报错:
The specified child already has a parent.
You must call removeView() on the child's parent first.
说孩子已经有一个父辈了,说明我们已经添加过了,此时是出现了重复添加。
让我们看下下面这行代码:
View view = inflater.inflate(R.layout.second_layout,container,false);
如图所示,attachToRoot这个参数表示是否添加到布局中去,而当我们没有写的时候是默认为true的,即自动添加到了布局中,添加对应布局不需要自动添加,因此在这里我们都将这个参数设置成false,这样一来就解决了这个问题。
和上文的代码类似,就是在通过inflater添加布局,而在参数设置中,
container之后多了一个false,就完美地解决了上述的报错
fragment布局,很简单只有一个TextView和一个Button
<?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"
android:background="#f000">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment"
android:gravity="center"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Main_Activity布局配置,在Fragment中进行匹配来显示想要得到的Fragment布局
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="193dp"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="@+id/fragment"
android:name="com.game.newfragmenttest.Fragment"//在此进行fragment的匹配
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
接下来是通过动态添加,用到replace这个方法
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义获取FragmentManager
FragmentManager fragmentManager = getSupportFragmentManager();
//使用FragmentTransaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//获取Fragment
Fragment fragment =new Fragment();
//利用fragmentTransaction的replace()方法,将fragment放入对应的framelayout中
fragmentTransaction.replace(R.id.fm,fragment);
//通过commit进入fragment生命周期进行操作
fragmentTransaction.commit();
}
}
使用FragmentManager的配置过程:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(mContainerId, mFragment);
以下是在布局中添加一个FrameLayout来显示动态添加的fragment
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="193dp"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/fm"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>