安卓Fragments入门

什么是Fragment

Fragment代表的是在一个Activity里面用户界面的一个行为或者一个部分。可以在一个Activity中组合多个Fragment或者在多个Activity中使用同一个Fragment。Fragment拥有自己的生命周期和事件体系,但位Activity之下,所以我们可以把它看做是一个子Activity。

Fragment受限于Activity

Fragment必须嵌入在Activity,并且Fragment的生命周期受其Host Activity直接影响,也就是Activity执行onPause()、onDestroy()等,Fragment随即也会执行。但在Activity正常运作中,Fragment则独立了。

添加方式

Fragment可以添加到Activity或者Viewgroup中,也可以没有界面而为Activity服务。

创建Fragment

要创建一个Fragment,必须先创建Fragment的子类(或它的一个现有的子类)。它含有类似Activity的回调方法,如oncreate(),onstart(),onpause(),和onstop()。
Fragment生命周期
生命周期如图所示,通常至少需要实现以下几种方法:
- onCreate():最先调用,执行一些初始化操作;
- onCreateView():在需要绘制界面时调用此方法,该方法需要返回一个View到该Fragment的根布局,不需要用户界面时返回null;
- onPause():当用户离开该Fragment时调用,并非destroyed,通常执行提交保存等操作,因为用户可能不再回到这个界面。

注意: 通常来说,实现以上三个方法就够了,如果要实现更多方法,参见Fragment生命周期图。还有一些可能用到的父类:
- DialogFragment
Displays a floating dialog. Using this class to create a dialog is a good alternative to using the dialog helper methods in the Activity class, because you can incorporate a fragment dialog into the back stack of fragments managed by the activity, allowing the user to return to a dismissed fragment.
- ListFragment
Displays a list of items that are managed by an adapter (such as a SimpleCursorAdapter), similar to ListActivity. It provides several methods for managing a list view, such as the onListItemClick() callback to handle click events.
- PreferenceFragment
Displays a hierarchy of Preference objects as a list, similar to PreferenceActivity. This is useful when creating a “settings” activity for your application.

添加用户界面

为了提供用户界面,需要实现onCreateView()方法,并且返回一个View给根布局。

public static class ExampleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.example_fragment, container, false);
    }
}

参数
- R.layout.example_fragment: Fragment的布局文件
- container: 需要传递到的根布局
- false: 是否将examlep_fragment附着到container上,此处已经附着于container故为false

将Fragment添加到Activity

有两种方式可以添加:

Layout布局文件方式

<?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:name="com.example.news.ArticleListFragment"
            android:id="@+id/list"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    <fragment android:name="com.example.news.ArticleReaderFragment"
            android:id="@+id/viewer"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
</LinearLayout>

注意: fragment 标签中 android:name属性指定的是Fragment类,系统在解析主布局xml时,依据该属性找到onCreateView()方法,获取View。

Java代码添加到ViewGroup方式

此方法可以在Activity运作中任意添加,即动态添加。在进行添加、删除或替换操作中,为了安全需要使用事务来进行操作。

// 开启事务
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// 执行添加操作
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment); // 父容器, Fragment
// 提交事务
fragmentTransaction.commit();

添加没有UI界面的Fragment: 调用add(Fragment, String)来添加,String标识唯一的ID来代替资源ID,并且系统将不会调用onCreate()方法,所以无须实现该方法。对于找到该Fragment,可以使用findFragmentByTag()来实现。

管理Fragment

在Activity中调用 getFragmentManager() 获得FragmentManager,其中包含方法 findFragmetById() 或者 findFragmentByTag() 用于获得Fragment,然后可以通过 popBackStack() 来弹出Fragment(模拟用户命令实现),还包含 addOnBackStackChangedListener() 方法。

addToBackStack()后允许用户点击返回键销毁该Fragment

执行其它事务操作

// 创建事务
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// 提交事务
transaction.commit();

Fragment与Activity之间的通信

两行代码看破一切:
Fragment中访问Activity

View listView = getActivity().findViewById(R.id.list);

Activity中访问Fragment

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);

回调法通信

public static class FragmentA extends ListFragment {
    ...
    // Container Activity must implement this interface
    public interface OnArticleSelectedListener {
        public void onArticleSelected(Uri articleUri);
    }
    ...
}

然后让Activity实现该接口以达到通知回调目的,为了确保Activity实现该接口,需要重写onAttach(activity)方法,这个方法在添加Fragment到Activity时调用,在该方法中实例化接口,如果Activity没有实现接口则会报错。然后,Fragment可以通过mListener发送消息回调到Activity了。

public static class FragmentA extends ListFragment {
    OnArticleSelectedListener mListener;
    ...
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnArticleSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
        }
    }
    ...
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值