前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >动态创建Fragment

动态创建Fragment

作者头像
仇诺伊
发布2018-09-12 14:21:47
2.3K0
发布2018-09-12 14:21:47
举报
文章被收录于专栏:佳爷的后花媛佳爷的后花媛

动态创建fragment的流程

1.0 新建一个类继承fragment.

2.0 在自定义的fragment里面复写oncreateVIew的方法

3.0 在onCreateVIew的方法里使用inflate填充器

4.0 通过Return方法把inflate得到View对象给返回出去

5.0 在使用fragment的activity里面调用getFragmentManager方法.得到fragmentManager对象

6.0 通过fragment管理对象,开启事务

7.0 使用事务对象,调用replace方法,替换fragment,是动态使用fragment精华

8.0 使用事务对象进行提交.

动态创建fragment的流程可以兼容低版本的安卓系统

1.0 导入包一律都是V4包下的

2.0 关于你们要使用到fragment的activity,一定要继承fragmentActivity

3.0 在或者fragment管理对象时,你们使用方法是getSupportFragmentManager静态方法.

fragment是activity的一部分,他依赖于Activity

fragment依赖于activity,不能单独存在,fragment的生命周期收到activity的生命周期的影响.

第一步,new class 继承 Fragment.

第二步,复写onCreateView的方法

第三步,在onCreateView方法里面进行,使用inflater把layout布局文件转换为一个View对象

第四步,在onCreateView的return方法里,把我们的View对象返回出去

第五步,在要使用activity的布局里面,像使用控件的方式把我们的fragment定义到ViewGroup(就是布局里面)

动态使用fragment的步骤:

第一步,new class 继承 Fragment

第二步,复写onCreateView方法

第三步,在onCreateView里面进行,使用inflater把layout布局文件转换为一个View对象

第四步.在onCreateVIew的return方法里,把我们的View对象返回出去

第五步.在java代码里通过静态方法getFragmentManager获取fragmentManager管理

第六步,通过fragmentManager的beginTransaction得到事务对象

第七步,通过事务对象调用.replace方法,替换控件为fragment

第八步,使用事务对象提交commit

v4兼容包下的fragment使用(现在开发基本不用了)

1.0 自定义fragment类里继承v4包下的fragment.记住所有用到fragment地方导入包必须一致

2.0 你们自定义的activity必须继承FragmentActivity

3.0 获取FragmentManager对象时,必须用getSupportFragmentManager方法.而不是getFragmentManager.

下面是我做的一个小Demo

是在一个页面中实现各个Activity之间的通信,左侧点击按钮,右侧出现相应的Activity界面.同时on关实现两个Activity之间的通信.

第一步,在布局文件main_Activity中设置按钮button和文本.然后加上布局文件FrameLayout.

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:orientation="horizontal"

tools:context=".MainActivity">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="vertical">

android:id="@+id/Btton_a1"

android:background="@drawable/bg"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/a1"/>

android:id="@+id/Btton_a2"

android:background="@drawable/bg"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/a2"/>

android:id="@+id/Btton_a3"

android:background="@drawable/bg"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/a3"/>

android:id="@+id/Btton_a4"

android:background="@drawable/bg"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/a4"/>

android:id="@+id/Btton_a5"

android:background="@drawable/bg"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/a5"/>

android:id="@+id/Btton_a6"

android:background="@drawable/bg"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/a6"/>

android:id="@+id/Btton_a7"

android:background="@drawable/bg"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/a7"/>

android:id="@+id/Activity_tv"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Activity传来的数据"/>

android:id="@+id/Activity_et"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="callMe"/>

android:id="@+id/Activity_callChild"

android:layout_width="wrap_content"

android:background="@drawable/bg"

android:layout_height="wrap_content"

android:text="@string/a8"/>

android:id="@+id/temp"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

在MainActivity中编写代码.

先编写一个方法initview()实现初始化.

然后根据swich开始编写Fragment.这里有七个Fragment,每个Fragment里面代码内容相似就用一个做代表.

这里Fragment1的Activity代码最为复杂.

private voidinitview() {

Btton_a1= (Button) findViewById(R.id.Btton_a1);

Btton_a2= (Button) findViewById(R.id.Btton_a2);

Btton_a3= (Button) findViewById(R.id.Btton_a3);

Btton_a4= (Button) findViewById(R.id.Btton_a4);

Btton_a5= (Button) findViewById(R.id.Btton_a5);

Btton_a6= (Button) findViewById(R.id.Btton_a6);

Btton_a7= (Button) findViewById(R.id.Btton_a7);

Activity_tv= (TextView) findViewById(R.id.Activity_tv);

Activity_et= (EditText) findViewById(R.id.Activity_et);

Activity_callChild= (Button) findViewById(R.id.Activity_callChild);

temp= (FrameLayout) findViewById(R.id.temp);

Btton_a1.setOnClickListener(this);

Btton_a2.setOnClickListener(this);

Btton_a3.setOnClickListener(this);

Btton_a4.setOnClickListener(this);

Btton_a5.setOnClickListener(this);

Btton_a6.setOnClickListener(this);

Btton_a7.setOnClickListener(this);

Activity_callChild.setOnClickListener(this);

}

@Override

public voidonClick(View v) {

fragmentManager=this.getFragmentManager();

FragmentTransaction beginTransaction =fragmentManager.beginTransaction();

switch(v.getId()) {

caseR.id.Btton_a1:

extracted();

break;

caseR.id.Btton_a2:

fragment2 fragement2 =newfragment2();

beginTransaction.replace(R.id.temp, fragement2);

break;

caseR.id.Btton_a3:

fragment3 fragment3 =newfragment3();

beginTransaction.replace(R.id.temp, fragment3);

break;

caseR.id.Btton_a4:

fragment4 fragment4 =newfragment4();

beginTransaction.replace(R.id.temp, fragment4);

break;

caseR.id.Btton_a5:

fragment5 fragment5 =newfragment5();

beginTransaction.replace(R.id.temp, fragment5);

break;

caseR.id.Btton_a6:

fragment6 fragment6 =newfragment6();

beginTransaction.replace(R.id.temp, fragment6);

break;

caseR.id.Btton_a7:

fragment7 fragment7 =newfragment7();

beginTransaction.replace(R.id.temp, fragment7);

break;

caseR.id.Activity_callChild:

break;

}

beginTransaction.commit();

}

Fragment1的布局代码和Activity代码:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:src="@drawable/a2"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textColor="#3C6"

android:text="

伫倚危楼风细细,望极春愁,

黯黯生天际。

草色烟光残照里,无言谁会凭栏意。

拟把疏狂图一醉,对酒当歌,

强乐还无味。

衣带渐宽终不悔,为伊消得人憔悴。"/>

android:id="@+id/fragment_tv"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Activity传来的数据"/>

android:id="@+id/fragment_et"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="callYou"/>

android:id="@+id/fragment_callChild"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="点击事件"/>

这里的Activity中加入了通信,要仔细看

packagefragment.com.hulufragment;

importandroid.app.Activity;

importandroid.app.Fragment;

importandroid.os.Bundle;

importandroid.support.annotation.Nullable;

importandroid.text.TextUtils;

importandroid.view.LayoutInflater;

importandroid.view.View;

importandroid.view.ViewGroup;

importandroid.widget.Button;

importandroid.widget.EditText;

importandroid.widget.TextView;

importandroid.widget.Toast;

/**

* Created by Administrator on 2016/10/1.

*/

public classfragment1extendsFragment {

privateActivityhomeActivity;

privateEditTextactivity_et;

privateViewview;

privateTextViewfragment_tv;

privateEditTextfragment_et;

privateTextViewactivity_tv;

@Nullable

@Override

publicView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

view= inflater.inflate(R.layout.fragment1,null);

homeActivity= getActivity();

ActivityInit();

fragemtnInit();

returnview;

}

private voidfragemtnInit() {

fragment_tv= (TextView)view.findViewById(R.id.fragment_tv);

fragment_et= (EditText)view.findViewById(R.id.fragment_et);

activity_tv= (TextView)homeActivity.findViewById(R.id.Activity_tv);

Button fragment_callChild = (Button)view.findViewById(R.id.fragment_callChild);

fragment_callChild.setOnClickListener(newView.OnClickListener() {

@Override

public voidonClick(View v) {

String trim =fragment_et.getText().toString().trim();

if(TextUtils.isEmpty(trim)){

Toast.makeText(homeActivity,"不能为空",Toast.LENGTH_LONG).show();

return;

}

activity_tv.setText(trim);

}

});

}

private voidActivityInit() {

Button Activity_callChild =(Button)homeActivity.findViewById(R.id.Activity_callChild);

activity_et= (EditText)homeActivity.findViewById(R.id.Activity_et);

Activity_callChild.setOnClickListener(newView.OnClickListener() {

public voidonClick(View v) {

String trim =activity_et.getText().toString().trim();

if(TextUtils.isEmpty(trim)){

Toast.makeText(homeActivity,"不能为空",Toast.LENGTH_LONG).show();

return;

}

fragment_tv.setText(trim);

}

});

}

}

其他的Fragment布局文件和Activity都相似:

packagefragment.com.hulufragment;

importandroid.app.Fragment;

importandroid.os.Bundle;

importandroid.support.annotation.Nullable;

importandroid.view.LayoutInflater;

importandroid.view.View;

importandroid.view.ViewGroup;

importandroid.widget.TextView;

/**

* Created by Administrator on 2016/10/1.

*/

public classfragment2extendsFragment {

privateTextViewtv_temp;

@Nullable

@Override

publicView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment2,null);

//fragment的布局控件的查找,就要用到inflater得到的VIew对象.

returnview;

}

}

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:src="@drawable/a2"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textColor="#3C6"

android:text="

伫倚危楼风细细,望极春愁,

黯黯生天际。

草色烟光残照里,无言谁会凭栏意。

拟把疏狂图一醉,对酒当歌,

强乐还无味。

衣带渐宽终不悔,为伊消得人憔悴。"/>

android:id="@+id/fragment_tv"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Activity传来的数据"/>

android:id="@+id/fragment_et"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="callYou"/>

android:id="@+id/fragment_callChild"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="点击事件"/>

这是我代码的地址,仅供参考.https://github.com/ZoeSj/FourFragment/tree/master/hulufragment

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.11.11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 下面是我做的一个小Demo
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档