http://www.tuicool.com/articles/FJ7VBb

FragmentTabHost切换Fragment时避免UI重新加载

不过,初次实现时发现有个缺陷,每次FragmentTabHost切换fragment时会调用onCreateView()重绘UI。

解决方法,在fragment onCreateView 里缓存View:

Java代码 收藏代码
private View rootView;// 缓存Fragment view

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
Log.i(TAG, "onCreateView");

if (rootView == null)
{
rootView = inflater.inflate(R.layout.fragment_1, null);
}
// 缓存的rootView需要判断是否已经被加过parent,如果有parent需要从parent删除,要不然会发生这个rootview已经有parent的错误。
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
{
parent.removeView(rootView);
}
return rootView;
}

http://www.yrom.net/blog/2013/03/10/fragment-switch-not-restart/

在项目中需要进行Fragment的切换,一直都是用replace()方法来替换Fragment:

1
2
3
4
5
6
7
8
9
    public void switchContent(Fragment fragment) {
if(mContent != fragment) {
mContent = fragment;
mFragmentMan.beginTransaction()
.setCustomAnimations(android.R.anim.fade_in, R.anim.slide_out)
.replace(R.id.content_frame, fragment) // 替换Fragment,实现切换
.commit();
}
}

但是,这样会有一个问题:
每次切换的时候,Fragment都会重新实例化,重新加载一边数据,这样非常消耗性能和用户的数据流量。

就想,如何让多个Fragment彼此切换时不重新实例化?

翻看了Android官方Doc,和一些组件的源代码,发现,replace()这个方法只是在上一个Fragment不再需要时采用的简便方法。

正确的切换方式是add(),切换时hide(),add()另一个Fragment;再次切换时,只需hide()当前,show()另一个。
这样就能做到多个Fragment切换不重新实例化:

1
2
3
4
5
6
7
8
9
10
11
12
    public void switchContent(Fragment from, Fragment to) {
if (mContent != to) {
mContent = to;
FragmentTransaction transaction = mFragmentMan.beginTransaction().setCustomAnimations(
android.R.anim.fade_in, R.anim.slide_out);
if (!to.isAdded()) { // 先判断是否被add过
transaction.hide(from).add(R.id.content_frame, to).commit(); // 隐藏当前的fragment,add下一个到Activity中
} else {
transaction.hide(from).show(to).commit(); // 隐藏当前的fragment,显示下一个
}
}
}

让多个Fragment 切换时不重新实例化、FragmentTabHost切换Fragment时避免UI重新加载的更多相关文章

  1. RadioGroup+Fragment 使用Fragment的add()方法,防止使用replace每次都重新加载页面,造成资源浪费

    radiogroup+fragment是很常用的主页导航控件,之前为了代码简便一直使用的replace()替换fragment,代码如下: getSupportFragmentManager().be ...

  2. 为什么说OC是运行时语言?什么是动态类型、动态绑定、动态加载?

    转载:https://www.cnblogs.com/dxb123456/p/5525343.html 动态: 主要是将数据类型的确定由编译时,推迟到了运行时. 这个问题其实浅涉及到两个概念,运行时和 ...

  3. Java MiniUi datagrid加载数据时,如果使用virtualScroll="false",数据多一点可能就会加载不出来

    datagrid的值为 virtualScroll="true" 问题解决.

  4. Android - FragmentTabHost 与 Fragment 制作页面切换效果

    使用 FragmentTabHost 与 Fragment 制作页面切换效果 API 19 TabHost已经不建议使用了.用 FragmentTabHost 来代替TabHost.实际上 Fragm ...

  5. ViewPager+Fragment取消预加载(延迟加载)(转)

    原文:http://www.2cto.com/kf/201501/368954.html 在项目中,都或多或少地使用的Tab布局,所以大都会用到ViewPager+Fragment,但是Fragmen ...

  6. ViewPager+Fragment取消预加载(延迟加载)

    在项目中,都或多或少地使用的Tab布局,所以大都会用到ViewPager+Fragment,但是Fragment有个不好或者太好的地方.例如你在ViewPager中添加了三个Fragment,当加载V ...

  7. 使用Maven加载项目有Dubbo框架时出现的常见异常情况

    异常描述:            The matching wildcard is strict, but no declaration can be found for element 'dubbo ...

  8. ListView用法及加载数据时的闪烁问题和加载数据过慢问题

    ListView介绍及添加数据时的闪烁问题 1.     ListView类 1.1 ListView常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示 ...

  9. 解决VS2012新建MVC3等项目时,收到加载程序集“NuGet.VisualStudio.Interop…”的错误

    vs2012来做一个mvc3的项目,哪知在创建ado数据模型时跳出这么一个东东 错 误: 此模板尝试加载组件程序集 “NuGet.VisualStudio.Interop, Version=1.0.0 ...

随机推荐

  1. DOM事件流

    DOM事件标准定义了两种事件流:Capture(捕获)和Bubbing(冒泡):捕获和冒泡是javascript针对dom事件处理的先后顺序,所谓的先后顺序是指针对父标签与其嵌套子标签,如果父标签与嵌 ...

  2. 一些不认识的开源js(更新ing。。。)

    孟星魂和小蝶归隐山林曾经说过,我们不问江湖事,但是不能不知道江湖事,因为我们是老伯的人(大概意思),所以有些东西可以用不到,但是一定要了解点... (首先不能人云亦云,但是有个主观观点也没啥大问题) ...

  3. 终端执行python shell的方法

    假设有一个Py文件,放在下PycharmProjects/learn下,文件名是 myfile.py. 1.打开终端输入python3进入2.在shell下 输入import sys 回车3.输入 s ...

  4. jquery学习笔记1

    (1) jQuery的Id选择器: $("#btnShow") (2) 事件绑定函数 bind() $("#btnAdd").bind("click& ...

  5. IO流--流转换

    import java.io.*; //键盘读入 字节流转换成字符流操作 提高效率 public class io { public static void main(String[] args) t ...

  6. 368. Largest Divisible Subset -- 找出一个数组使得数组内的数能够两两整除

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  7. Mybatis update In

    mysql语句如下: ,,) mybatis的mapper如下: int updateStateByIDs(@Param("ids") String[] ids, @Param(& ...

  8. target不起作用了

    原因是 <a href="",target></a>中间多了个逗号.

  9. SVMshow

    SVMshow % http://www.peteryu.ca/tutorials/matlab/visualize_decision_boundaries % load RankData % Num ...

  10. EF Code First 导航属性 与外键

    一对多关系 项目中最常用到的就是一对多关系了.Code First对一对多关系也有着很好的支持.很多情况下我们都不需要特意的去配置,Code First就能通过一些引用属性.导航属性等检测到模型之间的 ...