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的基础例子
随机推荐
- RoR - Expressing Database Relationships
One-to-One Association: *一个person只对应一个personal_info *一个personal_info只属于一个person *belongs to用foreign ...
- RVIZ实现模拟控制小车
RVIZ是一个强大的可视化工具,可以看到机器人的传感器和内部状态. 1.安装rbx1功能包Rbx1是国外一本关于ros的书中的配套源码,包含了机器人的基本仿真.导航.路径规划.图像处理.语音识别等等. ...
- 报错解决——xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
一般在遇到这个问题的时候都是想用git或者svn,结果发现用不了并报错xcrun: error: invalid active developer path (/Library/Developer/C ...
- [面试题]vi/vim快捷键及面试题系列
选择 vi保存退出命令 w! wq! q! www vi移动光标到文件最后一行 G g ggg 4444 vi删除一行的命令 dd d D shift+4 在vi编辑器中的命令模式下,键入()可在光标 ...
- mac系统上mysql开启外网访问
1.首先本地连到数据库在"mysql"库下把user表里User=root的那一行,把Hosts从"locahost"改成"%" 2.然后在 ...
- #WEB安全基础 : HTTP协议 | 0x7 学会使用wireshark分析数据包
wireshark是开源,免费,跨平台的抓包分析工具 我们可以通过wireshark学习HTTP报文和进行抓包分析,在CTF中的流量分析需要用到抓包 1.下载和安装 这是wireshark的官网 ht ...
- js 清空html input file的值
在做上传图片预览时,利用input onchange事件触发函数,但是type=file时,一定记得新建要清空原来的图片,因为原来的图片还存在在input里面,再选重复的图片没有change,故不会触 ...
- js cookie缓存处理
function setCookie(cnameList,cvalueList,exdays){ var d = new Date(); d.setTime(d.getTime()+(exdays*2 ...
- 虚拟机 安装centos
entOS安装 我是用VMware 12 安装的,下面是安装,打开VM主页 选择创建虚拟机 典型安装:VMwear会将主流的配置应用在虚拟机的操作系统上,对于新手来很友好. 自定义安装:自定义安装可以 ...
- Docker 部署 portainer
Docker 部署 portainer 环境: docker 版本 :18.09.1 主机地址:192.168.1.81 一.部署 porttainer 1.修改docker配置文件,开放端口. vi ...