Fragment碎片是可嵌入到活动中的UI片段,能让程序更加合理利用大屏幕。

Fragment基本用法

在一个活动中添加2个Fragment,这两个Fragment平方活动空间。
1、创建左侧的Fragment布局left_fragment.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:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Button"/>
</LinearLayout>

创建右侧的Fragment布局right_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#00ff00"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:text="This is right fragment."/>
</LinearLayout>

2、创建Fragment类
左侧Fragment类LeftFragment:

public class LeftFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.left_fragment, container, false);
        return view;
    }
}

右侧Fragment类RightFragment:

public class RightFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.right_fragment, container, false);
        return view;
    }
}

3、在活动布局中添加Fragment

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/left_fragment"
        android:name="com.jyoryo.app.android.study.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <fragment
        android:id="@+id/right_fragment"
        android:name="com.jyoryo.app.android.study.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
</LinearLayout>

动态添加Fragment

动态添加Fragment分为以下几个步骤:

  1. 创建待添加的Fragment
  2. 获取FragmentManager
  3. 开启事务fragmentManager.beginTransaction()
  4. 向容器添加或替换Fragment,一般使用transaction.replace(R.id.right_layout, fragment)
  5. 提交事务transaction.commit()
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replaceFragment(new AnotherRightFragment());
            }
        });
        replaceFragment(new RightFragment());
    }

    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.right_layout, fragment);
        transaction.commit();
    }

Fragment返回栈

在事务调用提交前调用addToBackStack方法,可以将一个事务添加至当前返回栈中。

    FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.right_layout, fragment);
        transaction.addToBackStack(null);
        transaction.commit();

FragmentActivity通信

Activity中获取Fragment

LeftFragment leftFragment = (LeftFragment)getSupportFragmentManager().findFragmentById(R.id.left_fragment);

Fragment中获取Activity

MainActivity activity = (MainActivity)getActivity();

Fragment生命周期

Fragment生命周期:

  • onAttach():当Fragment和活动建立关联的时候调用,被附加到Activity中去;
  • onCreate():在创建Fragment时调用此方法。可以初始化一段资源等;
  • onCreateView():为Fragment创建视图(加载布局)时调用;
  • onActivityCreated():确保与Fragment相关联的Activity一定已经创建完毕时调用;
  • onStart():当Fragment由不可见变为可见的时候调用;
  • onResume():FragmentActivity位于返回栈的栈顶,并且处于运行状态;
  • onPause():当Fragment被添加到返回栈后,被移除或替换时调用;
  • onStop():类似ActivityonStop()
  • onDestroyView():当FragmentActivity关联的视图被移除时调用;
  • onDestroy():
  • onDetach():当FragmentActivity解除关联的时候调用。

使用Fragment布局技巧

Android应用也可以在平板上运行,为了充分利用大屏幕,可以采用左右分屏模式(双页模式),但手机上的屏幕一次只能显示其中一页的内容,在手机上需要用两个页面分开进行显示。
但是如何在运行时确定是使用双页模式还是单页模式?可以借助限定符来实现。

在默认的布局文件夹layout下活动的布局activiy_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- layout 下的Activity布局 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/left_fragment"
        android:name="com.jyoryo.app.android.study.s0301.LeftFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

res目录下新建文件夹layout-large,创建活动的布局activiy_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- layout-large 下的Activity布局 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/left_fragment"
        android:name="com.jyoryo.app.android.study.s0301.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <fragment
        android:id="@+id/right_fragment"
        android:name="com.jyoryo.app.android.study.s0301.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
</LinearLayout>

上面2个布局就能实现默认屏幕大小应用使用单页模式,大屏幕使用双页模式。

Android中常见限定符
按屏幕大小划分:

限定符说明
small提供给小屏幕设备的资源
normal提供给中等屏幕设备的资源
large提供给大屏幕设备的资源
xlarge提供给超大屏幕设备的资源

按屏幕分辨率划分:

限定符说明
ldpi提供给低分辨率设备的资源(120dpi以下)
mdpi提供给中等分辨率设备的资源(120dpi~160dpi)
hdpi提供给高分辨率设备的资源(160dpi~240dpi)
xhdpi提供给超高分辨率设备的资源(240dpi~320dpi)
xxhdpi提供给超超高分辨率设备的资源(320dpi~480dpi)

按屏幕方向划分:

限定符说明
land提供给横屏设备的分辨率
port提供给竖屏设备的分辨率

标签: android

添加新评论