浅谈对Fragment的认识
首先声明我是一个菜鸟。下面也只是我个人的见解。
我觉得不管学习什么,要把握三个大的方向,一是,这个东东是什么,二是,这个东东怎么用,三是这个这个东东为什么这样用。
因为我也是一个初学者,对于第三个境界还达不到,只能勉强达到前面的两个。下面介绍一下fragment!
1.从网上看到,Fragment的出现是为了解决一个APP可以同时适应手机和平板而产生的,可以把他理解为Activity的界面的组成部分。
换句话说,再用到Fragment时,把Activity界面想象成是由Fragment以及其他控件组成的,你可以通过对Fragment的修改(比如动态添加,替换,删除某个fragment),完善相应功能。
2.如果你会使用了,上面说的就没什么用了,因为每个人的理解都不一样,还是说最关键的怎么使用吧!
在使用之前必须了解一下Fragment的生命周期,
Fragment必须依存于Activity的,Activity生命周期会直接影响到Fragmnet 的生命周期。
然后对他的生命周期简单了解:
onAttach(Activity) //这个是Fragment与Activity关联时调用,attach的汉语意思是伴随,从属,联在一起。
onCreateView() //创建Fragment视图
onActivityCreated() //当Activity中onCreated()方法返回时调用
onDestory() //当Fragment被移除时调用
onDetach() //当Activity和Fragment取消关联时调用
第一步:先在Activity布局文件中添加FragmentLayout,正常设置id,layout_height,layout_weight即可(这里添加Fragment也可以但是跟Fragment是有区别的,这个自己去查一下吧,属于细节)
<?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"> <FrameLayout android:id="@+id/frame" android:layout_weight="9" android:layout_height="wrap_content" android:layout_width="match_parent"/> <LinearLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/tv_boke" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="博客"/> <TextView android:id="@+id/tv_friend" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="好友"/> <TextView android:id="@+id/tv_setting" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="设置"/> </LinearLayout> </LinearLayout> 第二步,添加Fragment类继承Fragment,并重写OnCreateView方法,我这里添加了三个,分别对应上面的博客,好友,设置
BokeFragment
package com.example.administrator.fragment; import android.app.Fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * Created by Administrator on 2017-08-21 . */public class BokeFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.boke,container,false); }}FriendFragment
package com.example.administrator.fragment;import android.app.Fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * Created by Administrator on 2017-08-21 . */public class FriendFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.friend,container,false); }}SettingFragment
package com.example.administrator.fragment; import android.app.Fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * Created by Administrator on 2017-08-21 . */ public class SettingFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.setting,container,false); }}第三步:设置每个Fragment对应的布局;这里我列举一个博客的就OK,好友和设置跟博客的类似。
boke.xml
<?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"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:layout_gravity="center" android:text="我是痴王,我会加油!" android:textAlignment="center" android:textColor="#FF0000" /> </LinearLayout>第四步就是在Activity中对Fragment做需要的操作。
package com.example.administrator.fragment; import android.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView boke; private TextView friend; private TextView setting; private BokeFragment bokefragment; private FriendFragment friendfragment; private SettingFragment settingfragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); boke = (TextView) findViewById(R.id.tv_boke); friend = (TextView) findViewById(R.id.tv_friend); setting = (TextView) findViewById(R.id.tv_setting); boke.setOnClickListener(this); friend.setOnClickListener(this); setting.setOnClickListener(this); } public void onClick(View view){ android.app.FragmentManager fm = getFragmentManager(); android.app.FragmentTransaction transaction = fm.beginTransaction(); switch (view.getId()){ case R.id.tv_boke: bokefragment = new BokeFragment(); transaction.replace(R.id.frame,bokefragment); break; case R.id.tv_friend: friendfragment = new FriendFragment(); transaction.replace(R.id.frame,friendfragment); break; case R.id.tv_setting: settingfragment = new SettingFragment(); transaction.replace(R.id.frame,settingfragment); break; } transaction.commit(); }}下面是运行结果不会弄动态的图片,有人教教我吗?我就搞两个静态的图片吧1.点击博客出现的结果:
2.点击好友出现的界面:
转载于:https://www.cnblogs.com/chiwang/p/7410035.html
浅谈对Fragment的认识相关推荐
- 浅谈Android Fragment嵌套使用存在的一些BUG以及解决方法
自从Android3.0引入了Fragment之后,使用Activity去嵌套一些Fragment的做法也变得更加流行,这确实是Fragment带来的一些优点,比如说:Fragment可以使你能够将a ...
- 浅谈 LiveData 的通知机制
LiveData 和 ViewModel 是 Google 官方的 MVVM 架构的一个组成部分.巧了,昨天分析了一个问题是 ViewModel 的生命周期导致的.今天又遇到了一个问题是 LiveDa ...
- 浅谈Android保护技术__代码混淆
浅谈Android保护技术__代码混淆 浅谈Android保护技术__代码混淆 代码混淆 代码混淆(Obfuscated code)亦称花指令,是将计算机程序的代码,转换成一种功能上等价,但是难于阅读 ...
- 浅谈 URI 及其转义
浅谈 URI 及其转义 URI URI,全称是 Uniform Resource Identifiers,即统一资源标识符,用于在互联网上标识一个资源,比如 https://www.upyun.com ...
- android 存储空间监控,浅谈 Android 内存监控(中)
前言 在上篇 浅谈 Android 内存监控(上) 中,我们聊了 LeakCanary,微信的 Matirx 和美团的 Probe,它们各自有不同的应用场景,例如,在开发测试环境,我们会偏向用 Lea ...
- 浅谈人工智能:现状、任务、构架与统一
引言 第一节 现状:正视现实 第二节 未来:一只乌鸦给我们的启示 第三节 历史:从"春秋五霸"到"战国六雄" 第四节 统一:"小数据.大任务&q ...
- 浅谈Android Architecture Components
浅谈Android Architecture Components 浅谈Android Architecture Components 简介 Android Architecture Componen ...
- html 怪异模式,CSS_浅谈CSS编程中的怪异模式,怪异模式盒模型
今天学习了 - phpStudy...
浅谈CSS编程中的怪异模式 怪异模式盒模型 今天学习了一下css3的box-sizing属性,顺便又温习了一下css的盒模型,最后觉得有必要对盒模型做一个全面整理. 先不考虑css3的情况,盒模型一共 ...
- 浅谈 git 底层工作原理
浅谈 git 底层工作原理 系统复习到这里也快差不多了,大概就剩下两三个 sections,这里学习一下 git 的 hashing 和对象. 当然,跳过问题也不大. config 文件 这里还是会用 ...
最新文章
- RIA Service 的 SOAP EndPoint
- Loading class `com.mysql.jdbc.Driver'. This is deprecated警告处理
- Windows Phone UI控件
- asmr刷新失败无法连接上服务器_App Store显示无法连接怎么解决?两个步骤足够了...
- 计算机网络基础:Internet常用服务介绍​
- curl 升级 php,将命令行cURL转换为PHP cURL
- 【贪心】失意(jzoj 2318)
- html中css如何引用自定义字体 - 案例篇
- linux 批量删除进程的两种方法
- click事件的执行顺序
- wifi小程序源码流量主源码
- 单片机c语言论文参考文献,单片机应用程序论文,关于关于单片机应用编程的技巧相关参考文献资料-免费论文范文...
- 小米笔记本12.5java_小米12.5笔记本系统
- 初级办公计算机,初级(计算机办公软件应用)教案
- linux设置双屏拼接_双屏、3屏拼接——A卡、N卡——Windows、Linux
- vue-cli 项目启动输出 INFO Starting development server... 69o/o after emitting CopyPlugin
- adb 连接安卓手机远程调试
- 用WinSCP登录路由器并传入文件及改文件权限
- 风控每日一问:互联网金融产品如何利用大数据做风控?
- dedecms后台报错“Notice: Use of undefined constant MYSQL_ASSOC - assumed ‘MYSQL_ASSOC‘ ”的解决方法