作业七------Fragment
Fragment
一.为什么要使用fragment? 用来适配大屏幕
二.Minium Required SDK 要从API11 :Android 3.0开始因为从Android3.0以上才开始支持
三.如何使用fragment? fragment 理解成迷你activity,依托于activity存活,如果activity挂了,fragment也挂了。
1.写一个类继承Fragment
2.重写onCreateView()方法,返回当前fragment显示的view对象
public View onCreateView(LayoutInflater,ViewGroup container,Bundle savedInstanceState){ View v=inflater.inflate(R.layout.fragment,container,false); return v; }
3. 在activity获取到碎片化管理器FragmentManager
fm=getFragmentManager();
4.如果要更新界面要保证同时成功或者同时失败,开启事务
ft=fm.beginTransaction();
5.替换activity界面某个容器里面显示的内容(fragment)
ft.replace(R.id.container,new Fragment()); ft.commit();
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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=".MainActivity" android:orientation="horizontal" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <Button android:onClick="click1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮一" /> <Button android:onClick="click2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮二" /> <Button android:onClick="click3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮三" /> <Button android:onClick="click4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮四" /> </LinearLayout> <fragment android:name="cn.itcase.fragment.Fragment00" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/container" /> </LinearLayout>
package cn.itcase.fragment; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { private FragmentManager fm; private FragmentTransaction ft; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fm=getSupportFragmentManager(); } public void click1(View view){ ft= fm.beginTransaction(); ft.replace(R.id.container, new Fragment01()); ft.commit(); } public void click2(View view){ ft= fm.beginTransaction(); ft.replace(R.id.container,new Fragment02()); ft.commit(); } public void click3(View view){ ft= fm.beginTransaction(); ft.replace(R.id.container,new Fragment03()); ft.commit(); } public void click4(View view){ ft= fm.beginTransaction(); ft.replace(R.id.container,new Fragment04()); ft.commit(); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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=".MainActivity" android:background="#ffff00" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="界面一" /> </RelativeLayout>
package cn.itcase.fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.fragment.app.Fragment; public class Fragment01 extends Fragment { private View view; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.activity1, container, false); return view; } }