activity_main.xml
<?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="horizontal"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="100dp"
android:orientation="vertical"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_tom"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="20dp"
android:gravity="center"
android:text="Tom"/>
<Button
android:id="@+id/btn_jane"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="20dp"
android:gravity="center"
android:text="Jane"/>
</LinearLayout>
<FrameLayout
android:id="@+id/people"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
fragment_jane.xml
<?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:background="#0f0"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30sp"
android:text="Jane" />
</LinearLayout>
fragment_tom.xml
<?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:background="#f00"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30sp"
android:text="Tom" />
</LinearLayout>
FragmentJane.java
package com.jzl.fragmentdemo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.app.Fragment;
public class FragmentJane extends Fragment {
public FragmentJane()
{
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_jane, container, false);
return view;
}
}
FragmentTom.java
package com.jzl.fragmentdemo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.app.Fragment;
public class FragmentTom extends Fragment {
public FragmentTom()
{
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tom, container, false);
return view;
}
}
MainActivity.java
package com.jzl.fragmentdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//创建button对象
private Button btnTom;
private Button btnJane;
//创建fragment对象
private FragmentTom fragmentTom;
private FragmentJane fragmentJane;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定ID
bangID();
}
private void bangID() {
btnTom = findViewById(R.id.btn_tom);
btnJane = findViewById(R.id.btn_jane);
btnTom.setOnClickListener(this);
btnJane.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_tom:
//判断fragmentMan是否为空,无则创建fragment对象
if(fragmentTom ==null){
fragmentTom = new FragmentTom();
}
//创建FragmentManager对象
FragmentManager manager = getFragmentManager();
//创建FragmentTransaction事务对象
FragmentTransaction fragmentTransaction =manager.beginTransaction();
//使用replace(将要替换位置的i的,替换的页面)方法实现页面的替换
fragmentTransaction.replace(R.id.people, fragmentTom);
//提交事务
fragmentTransaction.commit();
break;
case R.id.btn_jane:
if(fragmentJane ==null){
fragmentJane = new FragmentJane();
}
FragmentManager manager1 = getFragmentManager();
FragmentTransaction fragmentTransaction1 =manager1.beginTransaction();
fragmentTransaction1.replace(R.id.people, fragmentJane);
fragmentTransaction1.commit();
break;
default:
break;
}
}
}