新建一个Fragment的过程
package com.example.myactivityiiii; 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 androidx.fragment.app.Fragment; /* * 新建一个Fragment的过程 * 1.自定义一个类,基础Fragment * 2.配置该类的layout文件 * 3.重写onCreateView方法,加载该类的布局文件,返回一个View * */ public class ListFragment extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate( R.layout.fragment_list,container,false ); return view; } }
package com.example.myactivityiiii; 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 androidx.fragment.app.Fragment; import java.util.zip.Inflater; public class DetailFragment extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate( R.layout.fragment_detail,container,false ); return view; } }
在Activity中添加Fragment
1.直接通过layout添加:在Main的布局文件中添加以下代码
<fragment android:layout_width="wrap_content" android:layout_height="match_parent" android:name="com.example.myactivityiiii.ListFragment" android:id="@+id/list" android:layout_weight="1"/> <fragment android:layout_width="wrap_content" android:layout_height="match_parent" android:name="com.example.myactivityiiii.DetailFragment" android:id="@+id/detail" android:layout_weight="2"/>
2.在Activity运行时添加Fragment,主要修改的是Main中的代码
在Main中的layout中添加一个布局容器,把新建的Fragment丢到这个布局容器里就好
Main的layout
<?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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Main" > </TextView> <FrameLayout android:id="@+id/frame" android:layout_width="wrap_content" android:layout_height="wrap_content" > </FrameLayout> </LinearLayout>
Main的java文件
package com.example.myactivityiiii; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; import android.os.Bundle; /* * Activity运行时添加Fragment * */ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DetailFragment detailFragment = new DetailFragment(); //获取Fragment的transaction实例 FragmentTransaction ft= getSupportFragmentManager().beginTransaction(); //指定一个容器来添加detailFragment ft.add(R.id.frame,detailFragment); //提交事物 ft.commit(); } }