Android基础-Fragment(碎片)详细介绍(一)

一、Fragment简介

Fragment是Android3.0以后引入的新的api,为了适配大屏的平板。

在普通手机开发的过程中,使用Fragment能实现一个界面的多次使用,能加快效率。Fragment可以被认为是Activity界面的一个布局,其依赖于Activity,但是拥有自己的活动事件与生命周期。可以通过替换Activity中的Fragment实现界面的优化处理。

现在Android提供一下两种包,在一个项目中最好使用同一个包下的Fragment,否则会出现一些不兼容问题,例如V4包不支持属性动画,app包下的不支持逐帧动画等

android.app.Fragment 兼容的最低版本是android:minSdkVersion=”11” 即3.0版。

android.support.v4.app.Fragment 兼容的最低版本是android:minSdkVersion=”4” 即1.6版。

二、Fragment使用方式

(一)静态使用

步骤:

创建fragment的xml

自定义Fragment继承自Fragment,在其中绑定fragment的视图,写fragment自己的事件

再Activity的xml中通过id引用该fragment

评价:

简单易用,直接引用对应的Fragment就好,但是如果一个屏幕中只有一个且需要切换Fragment就不方便使用。此时需要动态使用Fragment

例子:

新建一个左侧碎片布局 left_fragment.xml,代码如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
    <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="Button"
    />
</LinearLayout>

这个布局非常简单,只放置了一个按钮,并让它水平居中显示。然后新建右侧碎片布局right_fragment.xml,代码如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00ff"
android:orientation="vertical" >
    <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>

接着新建一个 LeftFragment 类,继承自 Fragment。注意,这里可能会有两个不同包下的 Fragment 供你选择,建议使用android.app.Fragment,因为我们的程序是面向 Android 4.0以上系统的,另一个包下的 Fragment 主要是用于兼容低版本的 Android 系统。

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;
    }
}

接着我们用同样的方法再新建一个 RightFragment,基本上代码都是相同的,这里就没有附上相应代码。

接下来修改 activity_main.xml中的代码,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
    <fragment
    android:id="@+id/left_fragment"
    android:name="com.example.fragmenttest.LeftFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

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

这里需要通过 android:name 属性来显式指明要添加的碎片类名,注意一定要将类的包名也加上。

这样最简单的碎片示例就已经写好了,现在运行一下程序,效果图如下:

(二)动态使用

步骤:

创建fragment的xml

自定义Fragment继承自Fragment,在其中绑定fragment的视图,写fragment自己的事件

在Activity的xml文件中加入一个空布局,用于加载fragment

在Activity的java文件中使用api中的方法引入fragment

评价:

能动态加载需要的布局,但是后面还能对其优化
 

例子:

我们还是在上面的代码基础上继续完善,新建 another_right_fragment.xml,代码如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff00"
android:orientation="vertical" >
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:textSize="20sp"
    android:text="This is another right fragment"
    />
</LinearLayout>

这个布局文件的代码和 right_fragment.xml 中的代码基本相同,只是将背景色改成了黄色,并将显示的文字改了改。然后新建 AnotherRightFragment 作为另一个右侧碎片,代码如下所示:

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

接下来我们将它动态地添加到活动当中。修改 activity_main.xml,代码如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <fragment
    android:id="@+id/left_fragment"
    android:name="com.example.fragmenttest.LeftFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

    <FrameLayout
    android:id="@+id/right_layout"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" >
        <fragment
        android:id="@+id/right_fragment"
        android:name="com.example.fragmenttest.RightFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    </FrameLayout>
</LinearLayout>

可以看到,现在将右侧碎片放在了一个 FrameLayout 中。 FrameLayout 是 Android 中最简单的一种布局,它没有任何的定位方式,所有的控件都会摆放在布局的左上角。由于这里仅需要在布局里放入一个碎片,因此非常适合使用FrameLayout。
之后我们将在代码中替换 FrameLayout 里的内容,从而实现动态添加碎片的功能。修改
MainActivity 中的代码,如下所示:

public class MainActivity extends Activity implements OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }
    @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button:
                    AnotherRightFragment fragment = new AnotherRightFragment();
                    FragmentManager fragmentManager = getFragmentManager();
                    FragmentTransaction transaction = fragmentManager.
                    beginTransaction();
                    transaction.replace(R.id.right_layout, fragment);
                    transaction.commit();
                    break;
                default:
                    break;
            }
        }
}

可以看到,首先我们给左侧碎片中的按钮注册了一个点击事件,然后将动态添加碎片的逻辑都放在了点击事件里进行。结合代码可以看出,动态添加碎片主要分为 5 步。
1. 创建待添加的碎片实例。
2. 获取到 FragmentManager,在活动中可以直接调用 getFragmentManager()方法得到。
3. 开启一个事务,通过调用 beginTransaction()方法开启。
4. 向容器内加入碎片,一般使用 replace()方法实现,需要传入容器的 id 和待添加的碎片实例。
5. 提交事务,调用 commit()方法来完成。
这样就完成了在活动中动态添加碎片的功能,重新运行程序,可以看到和之前相同的界面,然后点击一下按钮,效果如下图所示。

注意:如果我们想要在Fragment中实现返回栈效果

只在事务提交之前调用了 FragmentTransaction 的 addToBackStack()方法,它可以接收一个名字用于描述返回栈的状态,一般传入 null 即可。即

transaction.addToBackStack(null)。

三、碎片与活动之间的通信

(一)活动中获取碎片

Fragment fragment = (Fragment) getFragmentManager().findFragmentById(R.id.fragment);

  (二)   碎片中获取活动

MainActivity activity = (MainActivity) getActivity();

这里你可能会想,那么碎片和碎片之间可不可以进行通信呢?

说实在的,这个问题并没有看上去的复杂,它的基本思路非常简单,首先在一个碎片中可以得到与它相关联的活动,然后再通过这个活动去获取另外一个碎片的实例,这样也就实现了不同碎片之间的通信功能,因此这里我们的答案是肯定的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值