Fragment简介
碎片(Fragment)是一种可以嵌入到活动当中的UI片段,它能让程序更加合理和充分的利用大屏幕的空间。

Fragment的生命周期

它与Activity生命周期的关系:

可以看到Fragment比Activity多了几个额外的生命周期回调方法:
onAttach(Activity)
当Fragment与Activity发生关联时调用。
onCreateView(LayoutInflater, ViewGroup,Bundle)
创建该Fragment的视图
onActivityCreated(Bundle)
当Activity的onCreate方法返回时调用
onDestoryView()
与onCreateView想对应,当该Fragment的视图被移除时调用
onDetach()
与onAttach相对应,当Fragment与Activity关联被取消时调用
注意:除了onCreateView,其他的所有方法如果你重写了,必须调用父类对于该方法的实现,

Fragment的简单用法
步骤:
1、写一个layout,只放一个Button

<?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">
<Button
android:id="@+id/fragment_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="FragmentTest" />
</LinearLayout>
2,新建一个Fragment类,继承自supportV4 的Fragment

public class FragmentTest extends Fragment {
private Button mButtonTest;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.activity_fragmenttest,null);
mButtonTest= (Button) view.findViewById(R.id.button);
mButtonTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//点击按钮时会弹出一个Toast
Toast.makeText(getActivity(), "我点击了这个按钮", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
3,然后在activity_main中使用标签在布局中添加碎片

<fragment class="com.example.administrator.myfragment.fragment.MyFirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</fragment>
动态添加Fragment
例子:
状态图

步骤:
1,关于layout。
这个例子里我要添加了3个碎片,所以我写了三个不同layout。以及放fragment的activity_main。

第一个layout(fragmenttest)

<?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">
<Button
android:id="@+id/button"
android:text="测试按钮"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
第二个layout(fragment_second)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@mipmap/two"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textview"
android:text="我是第二个布局"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
第三个layout(fragment_third)

<?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">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imageview"
android:layout_gravity="center"
android:src="@mipmap/youtou"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

activity_mian

<RelativeLayout 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"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<LinearLayout
android:orientation="horizontal"
android:gravity="bottom|center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:text="fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button2"
android:text="fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button3"
android:text="fragment3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
2,新建三个class继承 Fragment重写onCreateView决定Fragemnt的布局
fragmentTest

public class FragmentTest extends Fragment {
private Button mButtonTest;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.activity_fragmenttest,null);
mButtonTest= (Button) view.findViewById(R.id.button);
mButtonTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//点击按钮时会弹出一个Toast
Toast.makeText(getActivity(), "我点击了这个按钮", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
fragmentSecond

public class FragmentSecond extends Fragment {

private TextView mTextView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_second,null);
mTextView= (TextView) view.findViewById(R.id.textview);
return view;
}
//修改文本框的内容
public void setText(String text){
if(mTextView!=null){
mTextView.setText(text);
}
}
}
fragmentThird

public class FragmentThird extends Fragment {
private EditText mEditText;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_third,null);
mEditText= (EditText) view.findViewById(R.id.editText);
return view;
}
//得到输入框文本的内容
public String getText(){
String s=null;
if(mEditText!=null){
s=mEditText.getText().toString();
}
return s;

}
}
3、在MainActivity中修改代码实现三个按钮的点击事件,通过按钮的点击事件来加载碎片
public class MainActivity extends FragmentActivity implements View.OnClickListener {
private Button mButton1;
private Button mButton2;
private Button mButton3;
private FragmentTest mFragmentTest;
private FragmentSecond mFragmentSecond;
private FragmentThird mFragmentThird;
private FragmentManager mFragmentManager;
private android.support.v4.app.FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton1= (Button) findViewById(R.id.button1);
mButton2= (Button) findViewById(R.id.button2);
mButton3= (Button) findViewById(R.id.button3);
mButton1.setOnClickListener(this);
mButton2.setOnClickListener(this);
mButton3.setOnClickListener(this);

mFragmentTest=new FragmentTest();
mFragmentSecond=new FragmentSecond();
mFragmentThird=new FragmentThird();
mFragmentManager=getSupportFragmentManager();
transaction=mFragmentManager.beginTransaction();
transaction.add(R.id.frame_container,mFragmentTest);
transaction.add(R.id.frame_container,mFragmentSecond);
transaction.add(R.id.frame_container,mFragmentThird);
transaction.hide(mFragmentTest);//隐藏界面
transaction.hide(mFragmentSecond);
transaction.commit();
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
transaction=mFragmentManager.beginTransaction();
// transaction.replace()把之前的页面删掉重新加载new FragmentTest()在FrameLayout上
// transaction.replace(R.id.frame_container,new FragmentTest());//frame_container是FrameLayout的id
transaction.hide(mFragmentSecond);
transaction.hide(mFragmentThird);
transaction.show(mFragmentTest);
transaction.commit();
break;
case R.id.button2:
transaction=mFragmentManager.beginTransaction();
// transaction.replace(R.id.frame_container,new FragmentSecond());
String text=mFragmentThird.getText();
mFragmentSecond.setText(text);
transaction.hide(mFragmentTest);
transaction.hide(mFragmentThird);
transaction.show(mFragmentSecond);
transaction.commit();

break;
case R.id.button3:
transaction=mFragmentManager.beginTransaction();
//transaction.replace(R.id.frame_container,new FragmentThird());
transaction.hide(mFragmentSecond);
transaction.hide(mFragmentTest);
transaction.show(mFragmentThird);
transaction.commit();
break;
default:
break;
}
}
}
---------------------
作者:团子11
来源:CSDN
原文:https://blog.csdn.net/tuanzi11/article/details/48473231
版权声明:本文为博主原创文章,转载请附上博文链接!

Android之Fragment(碎片)方方面面的更多相关文章

  1. Android Fragment(碎片)的使用

    简介 在Android中Fragment为一种可以嵌入活动中的UI片段.能让程序更加合理地利用大屏幕的空间. 使用方法 1.我们首先新建的一个onefragment.xml文件. <?xml v ...

  2. android UI:Fragment碎片

    碎片(Fragment) 嵌入与活动中的UI片段,为了合理的分配布局而存在,这是我的简单理解.多用于兼顾手机与平板的UI,也适用于灵活高级的UI制作. Demo 简单的按键切换两片不同的Demo 新建 ...

  3. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

  4. Fragment碎片

    布局文件中添加碎片 1.在onCteate()方法中调用inflater.inflate()加载Fragment布局 2.在xml的<fragment>中需要显示指明碎片名称(androi ...

  5. Android入门(六)碎片

    原文链接:http://www.orlion.ga/493/ 一.碎片 碎片(Fragment)是一种可以嵌入在活动当中的 UI片段,它能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用的非常 ...

  6. Android之Fragment学习笔记①

    Android Fragment完全解析,关于碎片你所需知道的一切 一. 什么是FragmentFragment(碎片)就是小型的Activity,它是在Android3.0时出现的.Fragment ...

  7. android 63 Fragment

    #Fragment 是3.0平板才引入进来的,3.0之后就加入了Fragment.原来是一个屏幕就是一个Activity,>片段,碎片 1. 定义某一个片段的界面 继承Fragment类 pub ...

  8. 14 Fragment 碎片总结

    Fragment 碎片 一, Fragment是什么? Android 3.0以后出现的 Api11 以上 Activity的组成部分 Fragment(小的Activity) Fragment可以显 ...

  9. android基础---->Fragment的使用

    碎片(Fragment)是一种可以嵌入在活动当中的UI 片段,它能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用的非常广泛. Fragment的基础例子

随机推荐

  1. 新建Maven项目时dtd约束出错

    新建或者导入Maven项目时出错:org.apache.maven.archiver.MavenArchiver.getManifesteclipse新建maven项目时,pom.xml文件第一行报错 ...

  2. Open_stack 有虚拟机端口不通的问题

    原因:流量表 解决办法:not修改流量表(容易出错,出错后果更严重):is迁移虚拟机这样会对应生成新的流量表,然后问题就解决了.

  3. python基础(5)-文件操作

    文件(file)操作 创建文件 verse.txt: 床前明月光 疑是地上霜 open(path(文件路径),mode(模式:r/r+[读],w/w+[写],a/a+[追加])):返回文件句柄(对象) ...

  4. mysql /tmp目录爆满问题的处理

    mysql /tmp目录爆满问题的处理 突然收到zabbix告警,说mysql服务器的/目录磁盘空间不足. 登录到服务器,看了下发现100GB的根目录,居然使用了差不多90GB.这台服务器上只跑了一个 ...

  5. 【JVM】-NO.113.JVM.1 -【JDK11 HashMap详解-0-全局-put】

    Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...

  6. Python基础(五) python装饰器使用

    这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 # -*- coding:gbk -*- '''示例1: 最简单的函数,表示调用了两次 ...

  7. 关于view.py 中 ajax json 的用法

    1. data=models.Citys.objects.filter(upid=0) data 的数据形式是一个查询集(也是一个列表,查询出来的每一条数据是一个对象): <QuerySet [ ...

  8. vue安装,router-link的一些属性,用法,tag active-class,to,replace,exex等等

    第一步:$ npm install -g vue-cli 第二部:$ vue init webpack my-projectName 下面内容转载自:https://www.cnblogs.com/c ...

  9. -bash: 未预期的符号 `(' 附近有语法错误

    [1]问题现象 -bash: 未预期的符号 `(' 附近有语法错误 [2]解决方案 给括号前面加反斜杠即可 Good Good Study, Day Day Up. 顺序 选择 循环 总结

  10. 怎样才能提交一个让开发人员拍手叫好的bug单

    怎样才能提交一个让开发人员拍手叫好的bug单 软件测试人员写得最多的文档就是测试用例和BUG,现在测试用例和BUG都没有标准的模板,每个公司使用的缺陷管理工具都有可能不一样,如果你换了一家公司就有可能 ...