Fragment

一、什么是Fragment?

1、Fragment是activity的界面中的一部分;多个Fragment们组合到一个activity中;多个activity中可重用一个Fragment。即Fragment相当于模块化的一段activity;
2、具有自己的生命周期,接收自己的事件;
3、在activity运行时被添加或删除。

二、为什么要使用Fragment?

1、支持更动态灵活的界面设计;2、activity的layout分成Fragment。

三、如何使用Fragment?

1、Create Fragment类 — onCreate() onCreateView() onPause()

2、Add Fragment

(1) Layout

<?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">

    <fragment
        android:id="@+id/fragment_test"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:name="com.example.chenjinhua.redcircle.FragmentTest"
        ></fragment>

</LinearLayout>

activity_fragment.xml布局文件里添加如上代码后,运行,报如下错误:非法状态异常,Fragment没有创建View。

这里写图片描述

解决方法:
首先新建一个view布局文件如之前第二周写的item_listview.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="5dp">

    <ImageView
        android:id="@+id/avatar_imageview"
        android:layout_width="50dp"
        android:layout_height="40dp"
        />
    <TextView
        android:id="@+id/item_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="name"
        android:layout_toRightOf="@+id/avatar_imageview"/>

    <TextView
        android:id="@+id/item_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="age"
        android:layout_below="@+id/item_name"
        android:layout_toRightOf="@+id/avatar_imageview"/>

</RelativeLayout>

然后在FragmentTest类的onCreateView()方法里获取view:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.item_listview,container);
        return view;
    }

(2)Java Code

FragmentManager;
FragmentTransaction;
Add/Remove

FragmentTestActivity类

package com.example.chenjinhua.redcircle;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;

/**
 * Created by chenjinhua on 16/3/19.
 */
public class FragmentTestActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        FragmentTest fragmentTest = new FragmentTest();
        fragmentTransaction.add(R.id.fragment_test_view,fragmentTest).commit();
    }
}

activity_fragment.xml布局文件:

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

    <fragment
        android:id="@+id/fragment_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.example.chenjinhua.redcircle.FragmentTest"
        ></fragment>

    <RelativeLayout
        android:layout_marginTop="100dp"
        android:id="@+id/fragment_test_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

FragmentTest类 同上。
运行报如下错误:

这里写图片描述

FragmentTestActivity类里fragment_test_view是父view;FragmentTest类里item_listview是子view。

解决办法:修改FragmentTest类里的onCreateView()方法

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //解析出来的xml视图放到container里,不绑定
        View view = inflater.inflate(R.layout.item_listview,container,false);
        return view;
    }

程序运行结果:
第一个Fragment是通过布局文件生成的,第二个Fragment是通过java code生成的。

这里写图片描述

删除fragment:修改FragmentTestActivity类代码为d

 fragmentTransaction.add(R.id.fragment_test_view,fragmentTest);
        fragmentTransaction.remove(fragmentTest).commit();

四、如何管理Fragment?

1、查找Fragment — findFragmentById() 和 findFragmentByTag()

    Fragment fragment = fragmentManager.findFragmentById(R.id.fragment_test);
        if (fragment instanceof FragmentTest){
            // TODO: Do your Action.
        }else{
            throw new IllegalStateException("iis not fragmentTest");
        }

java里面的二元运算符,判断左边的对象是否是右边类的实例。假如是的话,返回true;假如不是的话,返回false

2、Fragment的后退 — Fragment Stack; popBackStack(); addOnBackStackChangedListener()

五、Fragment 生命周期

参考博客:
http://blog.csdn.net/forever_crying/article/details/8238863/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值