Android--Fragment 碎片

一、静态注册fragment使用

首先需要创建Fragment的子类,在用fragment标签静态注册,使用name属性

1、创建Fragment
LeftFragment.java

package com.study.test.testapplication.fragment.mainfragment;

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

import com.study.test.testapplication.R;

/**
 * Create by BruceXuheng on 2018/6/1
 * description :
 **/

public class LeftFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.left_fragment,container,false);
        return view;

    }
}

RightFragment.java

package com.study.test.testapplication.fragment.mainfragment;

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

import com.study.test.testapplication.R;

/**
 * Create by BruceXuheng on 2018/6/1
 * description :
 **/

public class RightFragment extends Fragment {


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.right_fragment,container,false);
        return view;

    }
}

2、在一个活动中静态注册,使用name属性

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.study.test.testapplication.acty.MainFragmentActivity">
    <fragment
        android:id="@+id/left_fragment"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:name="com.study.test.testapplication.fragment.mainfragment.LeftFragment"
        />
    <fragment
        android:id="@+id/right_fragment"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:name="com.study.test.testapplication.fragment.mainfragment.RightFragment"
        />

</LinearLayout>

二、动态加载布局

1、准备两个Fragment,LeftFragment、RightFragment

package com.study.test.testapplication.fragment.mainfragment;

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

import com.study.test.testapplication.R;

/**
 * Create by BruceXuheng on 2018/6/1
 * description :
 **/

public class LeftFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.left_fragment,container,false);
        return view;

    }
}
package com.study.test.testapplication.fragment.mainfragment;

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

import com.study.test.testapplication.R;

/**
 * Create by BruceXuheng on 2018/6/1
 * description :
 **/

public class RightFragment extends Fragment {


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.right_fragment,container,false);
        return view;

    }
}

2、创建Activity

活动界面、创建两个FrameLayout

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.study.test.testapplication.acty.MainFragmentActivity">

    <FrameLayout
        android:id="@+id/left_frame_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        >
    </FrameLayout>

    <FrameLayout
        android:id="@+id/right_frame_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        >
    </FrameLayout>

</LinearLayout>

MainFragmentActivity活动源码:

package com.study.test.testapplication.acty;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import com.study.test.testapplication.R;
import com.study.test.testapplication.fragment.mainfragment.RightFragment;

/**
 * Create by BruceXuheng on 2018/6/1
 * description : 整理Fragmnet 碎片化整理学习
 **/

public class MainFragmentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_fragment);
        replaceFragment(new RightFragment());
        replaceLeftFragment(new RightFragment());
    }


    private void replaceFragment(Fragment fragment){

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transacion = fragmentManager.beginTransaction();
        transacion.replace(R.id.right_frame_layout,fragment);
        transacion.addToBackStack(null);
        transacion.commit();
    }
    private void replaceLeftFragment(Fragment fragment){

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

    }


}

从创建FragmentManager,在开启一个事务,事务将fragment和活动中的FrameLayout绑定,如果想添加到返回栈中,在事务中使用addToBackStack方法,再去提交。

这算是体验了碎片的使用,同样也具有生命周期,

三、如何灵活使用Fragment

  • onAttach()当碎片与活动建立关联调用
  • onCreateView()当碎片创建视图时调用
  • onActivityCreated()确保与碎片相关联的活动一定已经创建完毕的时候调用
  • onDestroyView()当与碎片关联的视图被移除的时候调用
  • onDetach()当碎片和活动解除关联的时候调用

完整生命周期可以从Android查看,或者随便百度Android Fragment生命周期

四、碎片可以适配机型横竖屏显示

在res下创建layout-large文件夹
当屏幕横屏的时候可以自动加载layout-large中的布局。要完整实现代码复杂度会比较高。

猜你喜欢

转载自blog.csdn.net/Mr_ChenXu/article/details/80540316