Android 之 Fragment

Fragment的由来:

1、基于Android系统的设备越来越多,分辨率种类越来越多,Google提出Fragment的概念也是希望通过Fragment 解决屏幕碎片化问题
2、Fragment最初有Google打包到V4的支持包中,Android4.0之后纳入sdk中。
3、 用Fragment替换TabHost是Google推荐的方法。

为什么使用Fragment:

1、通过使用奇幻activity或更换布局的方法,很难完美解决这些需求,Android3.0之后Google引入了Fragment(碎片)功能,在activity中实现不同界面的灵活切换,可以帮助程序员解决布局问题。
2、当一款APP同时推出手机和pad版,不需要再分别编写多套布局方案,通过Fragment设计布局可以动态灵活的解决不同屏幕分辨率的适配问题。

Fragment的优点:

1、使用Fragment可以在一个activity中实现不同界面的灵活切换
2、Fragment解决了activity间切换不流畅,布局切换时更轻量
3、Fragment可以封装成不同的重用组件,并可以单独管理其生命周期和UI布局
4、Fragment无需再AndroidManifest中注册,可以在布局文件中直接引用

Fragment的静态加载:

步骤:
新建类继承Fragment ;
重写方法 oncreatView方法;
使用layoutInflater对象中的inflate方法;
绑定布局和控件;
在activity对应的布局文件中通过标签引用.

实例:
这里写图片描述

第一步:首先在布局文件中创建一个fragment控件

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.dell.fragmented.MainActivity">

    <fragment
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

第二步:创建一个fragment布局文件Fragmentone

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我是Fragment" />
</LinearLayout>

第三步:创建一个类,继承Fragment,绑定布局

package com.example.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.dell.fragmentdemo.R;

/**
* Created by Dell on 2017/1/10.
*/

public class OneFragment extends Fragment {

    private View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //创建视图
        view = inflater.inflate(R.layout.fragment_one, null);
        return view;
    }
}

第四步,最重要:在Aactivity布局文件中的fragment控件中添加属性name

<fragment
    android:id="@+id/fragment"
    <--!属性-->
    android:name="com.example.fragment.OneFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Fragment的动态加载:

1、步骤:

* 新建类继承Fragment
* 重写方法oncreatView方法
* 使用layoutInflater对象中的inflate
* 方法绑定布局和控件
* 使用Fragmentmanager和Fragmenttransaction对象进行动态加载

2、FragmentManager:管理activity中的Fragment,需要使用FragmentManager,调用activity中的getFragmentManager()方法

3、FragmentTransaction:Fragment事务对象,对Fragment进行添加、移除、替换、提交等操作对象,常用方法如下
这里写图片描述

4、commit方法:
当所有准备工作完成后,必须调用commit方法才能执行和生效。

5、实例:
gif录制的不太好
这里写图片描述

第一步:修改布局文件:
这里写图片描述

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.dell.fragmented.MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="14dp"
            android:text="第一页面" />

        <TextView
            android:id="@+id/texttwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="14dp"
            android:text="第二页面" />

        <TextView
            android:id="@+id/texthree"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="14dp"
            android:text="第三页面" />

    </LinearLayout>

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"></FrameLayout>
</LinearLayout>

第二步:创建三个Fragment布局文件:(每个文件之设置背景色;fragmentone ,fragmenttwo ,fragmentthree)

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

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

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

</LinearLayout>

第三步:MainActivity

package com.example.dell.fragmentdemo;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.fragment.OneFragment;
import com.example.fragment.ThreeFragment;
import com.example.fragment.TwoFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //创建Fragment对象
    private Fragment fragone, fragtwo, fragthree;
    //创建Fragment管理
    private FragmentManager manager;
    //创建Fragment事务
    private FragmentTransaction transaction;
    //三个textview
    private TextView te1, te2, te3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //绑定id
        bindID();
        //实例化fragmentManager
        manager = getFragmentManager();
        //第一次载入第一个页面
        addPager();
    }

    private void addPager() {
        transaction = manager.beginTransaction();
        if (fragone == null) {
            fragone = new OneFragment();
        }
        transaction.replace(R.id.frameLayout, fragone);
        transaction.commit();
    }

    private void bindID() {
        te1 = (TextView) findViewById(R.id.textone);
        te2 = (TextView) findViewById(R.id.texttwo);
        te3 = (TextView) findViewById(R.id.texthree);
        //注册
        te1.setOnClickListener(this);
        te2.setOnClickListener(this);
        te3.setOnClickListener(this);
    }

    //监听
    @Override
    public void onClick(View v) {
        //实例化事务
        transaction = manager.beginTransaction();
        switch (v.getId()) {
            case R.id.textone:
                //切换到第一页面
                if (fragone == null) {
                    fragone = new OneFragment();
                }
                transaction.replace(R.id.frameLayout, fragone);
                break;
            case R.id.texttwo:
                //切换到第二页面
                if (fragtwo == null) {
                    fragtwo = new TwoFragment();
                }
                transaction.replace(R.id.frameLayout, fragtwo);
                break;
            case R.id.texthree:
                //切换到第三页面
                if (fragthree == null) {
                    fragthree = new ThreeFragment();
                }
                transaction.replace(R.id.frameLayout, fragthree);
                break;
        }
        //提交
        transaction.commit();
    }
}

说明:使用Fragmentmanager和Fragmenttransaction对象进行动态加载。

这样动态加载的效果就出来了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值