Fragment(2)构建一个灵活的UI,保存之前状态
来源:互联网 发布:多益网络下载 编辑:程序博客网 时间:2024/05/22 02:20
一个新闻软件,平板展示的是左边是标题,右边是新闻对应的内容。手机端就是第一个界面展示的是标题,点击进去就是新闻内容了。
FragmentManager 类提供add,remove,replace等方法,我们可以在Activity运行时动态添加移除替换Fragment。
在运行时添加一个Fragment到Activity中
我们要用FragmentManager来创建一个FragmentTransaction,来增删替换Fragment。如果我们要在Activity中动态替换Fragment的话,应该在onCreate()中初始化Fragment。
一个很重要的原则,当我们要动态添加Fragment的时候,我们在layout中必须要有一个View容器,来存放将要插入的Fragment的layout
xml布局代码:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" />
在Activity中,使用支持包的话就是调用 getSupportFragmentManager() 获得一个FragmentManager。接着调用 beginTransaction() 方法创建一个FragmentTransaction 并且调用add()方法来添加一个Fragment。
当我们已经完成了这些添加之类的操作,一定要记得提交事务。 调用的是FragmentTransaction 的 commit()方法。
代码里面动态添加:
import android.os.Bundle;import android.support.v4.app.FragmentActivity;public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.news_articles); // Check that the activity is using the layout version with // the fragment_container FrameLayout if (findViewById(R.id.fragment_container) != null) { // However, if we're being restored from a previous state, // then we don't need to do anything and should return or else // we could end up with overlapping fragments. if (savedInstanceState != null) { return; } // Create a new Fragment to be placed in the activity layout HeadlinesFragment firstFragment = new HeadlinesFragment(); // In case this activity was started with special instructions from an // Intent, pass the Intent's extras to the fragment as arguments firstFragment.setArguments(getIntent().getExtras()); // Add the fragment to the 'fragment_container' FrameLayout getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container, firstFragment).commit(); } }}因为Fragment是在运行时添加到一个FrameLayout容器中,而不是在xml中用<fragment>元素插入的,所以,我么可以动态的移除或者是替换掉Fragment。
我们要随时记住的是当我们完成Fragment事务,比如替换或者是移除一个Fragment,它常常应适当的允许用户导航返回和撤销之前的事情。为了允许用户导航返回通过我们的Fragment事务,我们必须在提交 FragmentTransation 之前调用 addToBackStack() 。即为允许用户点击返回键的时候,返回之前的Fragment。
!!!记住!!!当我们移除或者是替换一个Fragment并且把它的事务添加到回退栈的时候,这个被移除的Fragment它就是处于stopped的状态中,而不是destroyed。如果用户返回恢复之前的Fragment,它是restart的。如果没有把它的事务添加到回退栈之中的话,那么Fragment在移除或者是替换的时候,就是destroy的了。添加事务最大的好处就是它还存在,能在回调方法中进行返回数据的更改,又能记住状态。这个和那个Activity保存之前的状态是一个意思的。
替换其他的Fragment
官网代码:
// Create fragment and give it an argument specifying the article 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 view with this fragment,// and add the transaction to the back stack so the user can navigate backtransaction.replace(R.id.fragment_container, newFragment);transaction.addToBackStack(null);// Commit the transactiontransaction.commit();
addToBackStack()方法带有一个可选String参数,该String指定了一个唯一的事务名字。这个名字也不是必须除非我们想要完成之前的Fragment操作,可以使用FragmentManager.BackStackEntry来完成之前的Fragment操作。
- Fragment(2)构建一个灵活的UI,保存之前状态
- Training--使用Fragment创建动态UI(2)--构建一个灵活的UI
- Android学习路线(二十二)运用Fragment构建动态UI——构建一个灵活的UI
- 构建灵活的UI之Fragment(二)
- Android fragment(片段)构建灵活的UI
- 如何使用Fragment构建灵活的UI
- Android Developers:构建一个灵活的UI
- 构建灵活的UI
- Fragment的概念与使用(二)--构建灵活的Android应用UI
- Fragment的概念与使用(二)--构建灵活的Android应用UI
- Fragment的概念与使用(二)--构建灵活的Android应用UI
- Fragment的概念与使用(二)--构建灵活的Android应用UI
- Android官方开发文档Training系列课程中文版:使用Fragment构建动态UI之构建灵活的UI
- Fragment状态的保存
- Fragment的概念与使用--构建灵活的Android应用UI
- Android Training - 使用碎片创建一个动态UI(3) - 构建一个灵活的UI
- Android UI开发第三十篇——使用Fragment构建灵活的桌面
- Android UI开发第三十篇——使用Fragment构建灵活的桌面
- 查找技术思维导图
- HDU1312 Red and Black DFS
- 实验 三
- android adb启动失败问题 adb server is out of date. killing... ADB server didn't ACK * failed to start dae
- linux kernel 随记
- Fragment(2)构建一个灵活的UI,保存之前状态
- 安卓手机google商店的账号切换退出方式
- Android:30分钟弄明白Touch事件分发机制
- 圣诞福利到!51Testing邀你一起来狂欢!有礼就是任性~(≧▽≦)/~
- 基于PBOC的电子钱包消费交易过程
- linux学习(12)磁盘的分割、格式化、检验与挂载
- 《数据结构》实验三:栈和队列实验
- 基于PBOC的电子钱包的圈存过程
- 数据结构实验四