位置: 编程技术 - 正文

Fragment的使用(fragment详解)

编辑:rootadmin

推荐整理分享Fragment的使用(fragment详解),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:fragment from,fragment方法,fragment.,fragment详解,fragment detach,fragment作用,fragment作用,fragment的应用,内容如对您有帮助,希望把文章链接给更多的朋友!

当你的应用需要支持很多种屏幕尺寸,你可以在不同的布局配置中重用你的Fragment组件在可用的屏幕尺寸上优化用户体验。

例如,在手持设备中一次只显示一个Fragment可能是一个合适的选择,当然在Pad设备中将两个Fragment拼在一起来填满更大尺寸的屏幕,同时为用户显示更多的信息。

在上图中,两个Fragment,配合不同的配置显示在不同尺寸的屏幕上的效果。

FragmentManager类提供了运行时在Activity上添加,移除或者替换Fragment的方法,从而可以制造出动态的体验。

在运行的Activity中添加一个Fragment

你可以在运行中的Activity中添加Fragment,如果你想在Activity的生命周期中改变Fragment。

FragmentManager类提供了运行时在Activity上添加,移除或者替换Fragment的方法,从而可以制造出动态的体验。

如果你的Activity允许Fragment被移除和替换,你需要在Activity的onCreate方法中添加初始化的Fragment(s)。

Fragment的使用(fragment详解)

一个处理Fragment的重要原则,Fragment必须有一个在布局中有一个让其寄存的View容器,特别是对那些运行时的Fragment。

以下是一个布局文件,每次显示一个Fragment。为了用一个Fragment替换另外一个,Activity的布局文件包含一个空的FrameLayout,作为Fragment容器使用。

res/layout/news_articles.xml:<FrameLayoutxmlns:android=" android:id="@&#;id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" />

.

在Activity当中,利用getSupportFragmentManager()获取一个FragmentManager,以下是代码示例:

import android.os.Bundle;import android.support.v4.app.FragmentActivity; public class MainActivity extends FragmentActivity { @Override public voidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.news_articles); // Checkthat the activity is using the layout version with // thefragment_container FrameLayout if(findViewById(R.id.fragment_container) != null) { //However, if we're being restored from a previous state, // thenwe don't need to do anything and should return or else // wecould end up with overlapping fragments. if(savedInstanceState != null) { return; } // Create an instance of ExampleFragment HeadlinesFragment firstFragment = new HeadlinesFragment(); // Incase this activity was started with special instructions from an Intent, // passthe Intent's extras to the fragment as arguments firstFragment.setArguments(getIntent().getExtras()); // Addthe fragment to the 'fragment_container' FrameLayout getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container,firstFragment).commit(); } }}

Fragment替换

替换Fragment的流程和添加的流程类&#;,只不过需要利用replace方法替换add方法。

需要记住的是,当你是Fragment切换的时候,例如替换或者一处,最好让用户拥有返回或者撤销的机会

当你允许用户返回或者撤销Fragment操作的时候,你必须在调用FragmentTransaction之前调用addToBackStack。

以下是一个示例:

// Create fragment and give it an argument specifying thearticle it should showArticleFragment newFragment = new ArticleFragment();Bundle args = new Bundle();args.putInt(ArticleFragment.ARG_POSITION, position);newFragment.setArguments(args); FragmentTransaction transaction =getSupportFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container viewwith this fragment,// and add the transaction to the back stack so the usercan navigate backtransaction.replace(R.id.fragment_container,newFragment);transaction.addToBackStack(null); // Commit the transactiontransaction.commit();

文章转自:

Android基础教程(七)之----单选项框RadioGroup的综合应用 转自:

Fragment生命周期 官网帮助文档链接:

Android的事件处理-android学习之旅(四十四) androd事件处理简介控制飞机移动的简单实例packagepeng.liu.test;importandroid.app.ActionBar;importandroid.app.Activity;importandroid.graphics.Color;importandroid.os.Bundle;importandroid.

标签: fragment详解

本文链接地址:https://www.jiuchutong.com/biancheng/386770.html 转载请保留说明!

上一篇:android退出整个应用程序(android退出程序代码)

下一篇:Android基础教程(七)之----单选项框RadioGroup的综合应用(android基础教程第三版电子版)

  • 企业销售固定资产车辆怎么开票
  • 私车公用车险是个人名字可报销吗
  • 公司户和个人户卖车的时候价格差多少
  • 党建经费的标准
  • 预提费用在资产负债表哪个科目
  • 借贷必相等为什么是错的
  • 企业申报表有异常期间可以更换财务负责人吗
  • 收到订金如何开票
  • 计提坏账准备怎么理解
  • 个税累计免征额
  • 单位支付给职工的一次性补助金怎么入账
  • 会计核算制度的案例
  • 购买理财产品现金流量处理怎么做?
  • 应付职工薪酬包括个税吗
  • 商户办会员卡 商场承担责任
  • 进项税转出的附加税怎么做
  • 营改增后建筑公司自建行为所得税
  • 运输营改增
  • 建筑施工企业购进材料会计分录
  • 资产负债表和利润表在哪里查
  • 购买股票的分录怎么写
  • 企业购入土地如何处理
  • 银行承兑第一手怎么盖章
  • 税金及附加包括地方教育费附加吗
  • 付款后多久开发发票
  • 试运营收入账务处理
  • 应发工资包含罚款吗
  • Win10任务栏图标居中
  • win11比win10是更流畅了吗
  • 商业承兑汇票承兑人可以是银行吗
  • sadge什么意思
  • 索尼vpcea200c笔记本
  • 本月暂估成本的会计分录
  • kb4586853更新
  • win10系统如何更改工作组
  • 委托加工物资企业收回自用
  • 普通发票作废后能取消作废吗
  • 弥补以前年度亏损最多几年
  • 国有资产无偿划转协议
  • 体积最小的u盘
  • 收入支出如何算利润
  • php实现和工作原理
  • 增值税专用发票怎么开
  • 前端进阶路线
  • python合并多个excel
  • 预缴的附加税需要转出吗
  • 土地出让金抵减增值税账务处理
  • 当月销售次月开票就按次月申报
  • 车间领用材料应编制什么凭证
  • 没有数字的公章有效吗
  • 营业额和营业收入怎么算
  • 根据工资总额组成的规定下列哪些列入工资总额的范围
  • 个体工商户是什么意思
  • 存货跌价准备确认
  • 现金解款单的会计分录
  • 更衣柜属于什么费用
  • 化妆品消费税纳税标准
  • 员工在异地缴纳社保,公司还能上吗
  • 先收发票后付款怎么做分录
  • 为什么要挂靠其他劳务派遣公司?有什么利弊?
  • 材料采购办法
  • 新会计准则应用
  • solaris 修改用户 主目录
  • 尽量别熬夜
  • ac1st19.dll
  • task运行在哪个工作单元
  • dgservice.exe是什么
  • kmswin7激活步骤
  • w10自启
  • unity as
  • dos echo命令
  • perl正则表达式
  • vim fold
  • unity linux arm
  • 安卓系统引导程序
  • javascript函数大全
  • 进项税额转出影响所得税吗
  • 河北地方税务局网
  • 国家税务总局里面怎么改办税人
  • 进项税留抵怎么消化掉
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设