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. Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:443

    (1)端口被占用,找到对应的进行并结束.(2)Linux下查看,无进程占用443端口,确认/etc/httpd/conf.d下只有一个ssl.conf,无其他SSL配置.备份文件,如果有,apache ...

  2. tensorflow 的数据管理

    tensorflow api操纵和管理的是numpy矩阵数据 例子: import tensorflow as tf import numpy as np vector_np = np.array([ ...

  3. [openjudge-贪心]删数问题

    题目描述 题目描述 键盘输入一个高精度的正整数N,去掉其中任意k个数字后剩下的数字按原左右次序将组成一个新的正整数.编程对给定的N和k,寻找一种方案使得剩下的数字组成的新数最小. 输出应包括所去掉的数 ...

  4. 转:C#中Undo/Redo的一个简易实现

    一个比较常见的改进用户体验的方案是用Redo/Undo来取代确认对话框,由于这个功能比较常用,本文简单的给了一个在C#中通过Command模式实现Redo/Undo方案的例子,以供后续查询. clas ...

  5. 我设计的电脑usb红外遥控键盘原理图

    我设计的电脑usb红外遥控键盘,orcad原理图备份如下:

  6. C/C++ strict-aliasing

    最近发现了一个奇怪的编译参数-fno-strict-aliasing,好奇之下做了一点研究: 重点参考Understanding C/C++ Strict Aliasing: 所谓的aliasing就 ...

  7. Linux+Jenkins环境搭建

    一.安装基础环境 1. yum -y install java-1.8.0-openjdk.x86_64 #安装1.8jdk 2. 查看java 版本 [root@localhost djanggo_ ...

  8. three.js 3d三维网页代码加密的实现方法

    http://www.jiamisoft.com/blog/17827-three-js-3dsanweiwangyejiami.html https://www.html5tricks.com/ta ...

  9. bzoj3437 小P的牧场(斜率优化dp)

    3437: 小P的牧场 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2025  Solved: 1110[Submit][Status][Discu ...

  10. rest_framework视图和组件

    一.视图 1.基本视图 #基本视图 #抽取基类 from rest_framework.response import Response from rest_framework.views impor ...