MainActivity
package com.example.fragment;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
private Afragment afragment;
private Bfragment bfragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//replace是替换
//先实例化Afragment
afragment=new Afragment();
//通过add方法将fragment加载到main的布局文件指定的位置去,记得调用commit
getFragmentManager().beginTransaction().add(R.id.cun_fragment,afragment).commitAllowingStateLoss();
button=findViewById(R.id.buttonPanel);
//设置点击事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(bfragment==null)
bfragment=new Bfragment();
//跳转到afragment
getFragmentManager().beginTransaction().replace(R.id.cun_fragment,bfragment).commitAllowingStateLoss();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转"
android:id="@+id/buttonPanel"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cun_fragment"
android:layout_below="@+id/buttonPanel"/>
</RelativeLayout>
Afragment
package com.example.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class Afragment extends Fragment {
private TextView textView1;
@Nullable
@Override
//通过这个方法可以返回一个视图文件
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.a_fragment,container,false);
return view;
}
//可以在这个里面做一些事情
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
textView1=view.findViewById(R.id.a_text1);
}
}
a_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/a_text1"
android:textColor="@color/black"
android:gravity="center"
android:text="this is a fragment"/>
</LinearLayout>
Bfragment
package com.example.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class Bfragment extends Fragment {
private TextView textView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.b_fragment,container,false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
textView=view.findViewById(R.id.b_text1);
}
}
b_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/b_text1"
android:textColor="@color/black"
android:gravity="center"
android:text="this is b fragment"/>
</LinearLayout>