Android之Fragment(碎片)方方面面
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(碎片)方方面面的更多相关文章
- Android Fragment(碎片)的使用
简介 在Android中Fragment为一种可以嵌入活动中的UI片段.能让程序更加合理地利用大屏幕的空间. 使用方法 1.我们首先新建的一个onefragment.xml文件. <?xml v ...
- android UI:Fragment碎片
碎片(Fragment) 嵌入与活动中的UI片段,为了合理的分配布局而存在,这是我的简单理解.多用于兼顾手机与平板的UI,也适用于灵活高级的UI制作. Demo 简单的按键切换两片不同的Demo 新建 ...
- Android中Fragment与Activity之间的交互(两种实现方式)
(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...
- Fragment碎片
布局文件中添加碎片 1.在onCteate()方法中调用inflater.inflate()加载Fragment布局 2.在xml的<fragment>中需要显示指明碎片名称(androi ...
- Android入门(六)碎片
原文链接:http://www.orlion.ga/493/ 一.碎片 碎片(Fragment)是一种可以嵌入在活动当中的 UI片段,它能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用的非常 ...
- Android之Fragment学习笔记①
Android Fragment完全解析,关于碎片你所需知道的一切 一. 什么是FragmentFragment(碎片)就是小型的Activity,它是在Android3.0时出现的.Fragment ...
- android 63 Fragment
#Fragment 是3.0平板才引入进来的,3.0之后就加入了Fragment.原来是一个屏幕就是一个Activity,>片段,碎片 1. 定义某一个片段的界面 继承Fragment类 pub ...
- 14 Fragment 碎片总结
Fragment 碎片 一, Fragment是什么? Android 3.0以后出现的 Api11 以上 Activity的组成部分 Fragment(小的Activity) Fragment可以显 ...
- android基础---->Fragment的使用
碎片(Fragment)是一种可以嵌入在活动当中的UI 片段,它能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用的非常广泛. Fragment的基础例子
随机推荐
- Docker入门4------Dockerfile
转自:https://www.cnblogs.com/jsonhc/p/7766841.html https://www.cnblogs.com/jsonhc/p/7767669.html Docke ...
- Docker入门2------容器container常规操作
参考转自 https://www.cnblogs.com/jsonhc/p/7760144.html Docker的container 运行一个container的本身就是开启一个具有独立namesp ...
- AngularJs实现全选功能
html代码 <!-- 数据表格 --> <div class="table-box"> <!--工具栏--> <div class=&q ...
- python 模块大全
logging time datetime sys os json random hashlib paramiko pymysql模块使用 subprocess pywi ...
- safari手机浏览器的width:100%的自适应问题
Tips: 调试 iPad 或 iPhone 可在设置中启动调试模式,在 Mac 中的 Safari 浏览器 同样开启开发者模式后,进行联机调试.功能彪悍. 最近在做一个页面时,发现在 iPad 的 ...
- 【Java】NO.83.Tool.1.GlassFish.1.001-【GlassFish 5 安装使用】-
1.0.0 Summary Tittle:[Java]NO.83.Tool.1.GlassFish.1.001-[GlassFish 5 安装使用]- Style:EBook Series:Java ...
- 'webpack-dev-server' 不是内部或外部命令,也不是可运行的程序
npm install webpack-dev-server --save
- python数据结构-如何快速找到多个字典中的公共键
如何快速找到多个字典中的公共键 问题举例 统计每轮都进球的球员: 第1轮{‘tom’:1, 'meixi':2} 第2轮{‘coco’:3, 'meixi':4, 'marton':2} 第3轮{'c ...
- gitlab的ssh key有2个
Gitlab添加SSH key可以pull不能push的问题 最后解决的是 使用http去clone pull 提交 没用ssh.就是需要输入密码
- Ocelot:API网关概要
一.概要 Ocelot是.Net Core下一个开源API网关:Ocelot主要目标是在.NET在微服务或面向服务架构中提供统一的入口服务, Ocelot拿到HttpRequest对象到管道后,先创建 ...