首先是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="vertical" tools:context=".MainActivity"> <Button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="replace"/> <Button android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="change"/> <!-- 为了动态设置,添加一个FrameLayout--> <FrameLayout android:id="@+id/fragmentLayout" android:background="#ff00ff" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout> </LinearLayout>
Fragment.xml没啥东西
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Fragment1"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffff00" android:text="@string/hello_blank_fragment" /> </FrameLayout>
Fragment.class其实也没有东西
package com.example.myfragment; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment1 extends Fragment { public Fragment1() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_1, container, false); } }
MainActivity.class
package com.example.myfragment; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button btn1; Button btn2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = findViewById(R.id.btn1); btn2 = findViewById(R.id.btn2); //btn1事件监听 btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //点击这个按钮,把Fragment1替换到FrameLayout上 replaceFragment(new Fragment1()); } }); //btn2事件监听 btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { } }); } //动态切换fragment private void replaceFragment(Fragment fragment) { //获取FragmentManger管理类 FragmentManager fragmentManager = getSupportFragmentManager(); //获取Fragment的触发器 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); //Fragment的替换动作由fragmentTransaction完成 fragmentTransaction.replace(R.id.fragmentLayout,fragment); //提交这个事件 fragmentTransaction.commit(); } }
效果图如下:
原文:https://www.cnblogs.com/tuyaojiao/p/15237278.html