调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment之我的解决方法
private class ViewPagerAdapter extends FragmentPagerAdapter {
FragmentManager mFragmentManager;
FragmentTransaction mCurTransaction;
public ViewPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
this.mFragmentManager = fragmentManager;
}
@Override
public Fragment getItem(int position) {
final String mimeType = mSortedActionMimeTypes.get(position);
Log.d("lyl", "getItem-->mimeType: " + mimeType + " position: " + position);
QuickContactListFragment fragment = new QuickContactListFragment(mimeType);
return fragment;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
removeFragment(container,position);
QuickContactListFragment fragment =
(QuickContactListFragment) super.instantiateItem(container,position);
final String mimeType = mSortedActionMimeTypes.get(position);
final List<Action> actions = mActions.get(mimeType);
Log.d("lyl", "instantiateItem-->mimeType: " + mimeType + " position: " + position
+ " container.id: " + container.getId());
Log.d("lyl", "actions: " + actions);
fragment.setActions(actions);
return fragment;
}
private void removeFragment(ViewGroup container, int position){
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
String name = getFragmentName(container.getId(), position);
Fragment fragment = mFragmentManager.findFragmentByTag(name);
mCurTransaction.remove(fragment);
mCurTransaction.commit();
mCurTransaction = null;
mFragmentManager.executePendingTransactions();
}
private String getFragmentName(int viewId, int index) {
return "android:switcher:" + viewId + ":" + index;
}
@Override
public int getCount() {
return mSortedActionMimeTypes.size();
}
@Override
public int getItemPosition(Object object) {
final QuickContactListFragment fragment = (QuickContactListFragment) object;
final String mimeType = fragment.getMimeType();
for (int i = 0; i < mSortedActionMimeTypes.size(); i++) {
if (mimeType.equals(mSortedActionMimeTypes.get(i))) {
return i;
}
}
return PagerAdapter.POSITION_NONE;
}
}
贴出源代码FragmentPagerAdapter中关于instantiateItem方法的机制
@Override
public ObjectinstantiateItem(View container, int position) {
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
// Do we already have this fragment?
String name = makeFragmentName(container.getId(), position);
Fragment fragment = mFragmentManager.findFragmentByTag(name);
if (fragment != null) {
if (DEBUG) Log.v(TAG, "Attaching item #" + position + ": f=" + fragment){
mCurTransaction.attach(fragment);
} else {
fragment = getItem(position);
if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
mCurTransaction.add(container.getId(), fragment,
makeFragmentName(container.getId(), position));
}
if (fragment != mCurrentPrimaryItem) {
fragment.setMenuVisibility(false);
}
return fragment;
}
在继承fragmentpagerAdapter的类中。重载instantiateItem后,首先清除掉缓存内的fragment,这样保证了调用父类的instantiateItem后,会每次从geiItem()里获得最新的fragment.就攻克了使用的是缓存内的fragment,而数据却不是最新的问题。
调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment之我的解决方法的更多相关文章
- 【转】为什么调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment?
为什么调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment? 转自:http://www.apkbus.com/android- ...
- 为什么调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment?
在一个 Android 应用中,我使用 FragmentPagerAdapter 来 处理多 Fragment 页面的横向滑动.不过我碰到了一个问题,即当 Fragment 对应的数据集发生改变时,我 ...
- 为什么调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment?【转载】
转载自:http://www.cnblogs.com/dancefire/archive/2013/01/02/why-notifyDataSetChanged-does-not-work.html ...
- 为什么调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment
http://stackoverflow.com/questions/10849552/update-viewpager-dynamically if you want to switch out t ...
- mssql sqlserver 表增加列后,视图不会自动更新相关列的两种解决方法分享
摘要: 今天对物理数据表,进行增加列操作后,程序一直显示无法找到相应列,通过仔细比对发现,视图中无相应列更新,下文将具体的解决方法分享如下: 例: create view vw_test as sel ...
- Win8.1无法安装更新,提示0x800*****错误的解决方法
Win8.1无法安装更新,提示0x800*****错误的解决方法 注:本教程同样适用于Win10系统 有时候Win8.1某个系统文件的损坏会导致无法安装Windows更新,表现为Windows更新 ...
- 记前些日子archlinux更新后无法调节声音的解决方法
桌面环境用的是xfce4. 自从某次更新过后,panel中调节声音的插件变成了 xfce4-pulseaudio-plugin.然后就发现在panel中无法调节声音了. 在这个插件的属性中发现了一项设 ...
- 【我的Android进阶之旅】Android调用JNI出错 java.lang.UnsatisfiedLinkError: No implementation found for的解决方法
错误描述 今天使用第三方的so库时候,调用JNI方法时出现了错误.报错如下所示: 11-01 16:39:20.979 4669-4669/com.netease.xtc.cloudmusic E/a ...
- Win10更新后蓝牙出现故障的解决方法
昨天Win10自动更新后,我发现我的键盘突然就不管用了,检查了一下发现原来蓝牙没有打开,同时任务栏中的蓝牙图标也不见了. 不久之前,这样的情况已经出现过了一次,那次好像更新系统后就好了,但这次是系统更 ...
随机推荐
- 第四种:GCD
GCD 1> 概述 Grand Central Dispatch (GCD)是Apple开发的一种多核编程技术.主要用于优化应用程序以支持多核处理器以及其他对称多处理系统. GCD提供函数实现多 ...
- hibernate+spring的整合思路加实例(配图解)
首先框架整合我感觉最难的是jar包的引入.因为不同框架的jar容易产生冲突.如果能排除这个因素我想说整合框架还是相对比较容易的. 我整合的框架的一个思想就是:各司其职.因为每个框架处理的事务或者是层次 ...
- Java线程(十):CAS
前言 在Java并发包中有这样一个包,java.util.concurrent.atomic,该包是对Java部分数据类型的原子封装,在原有数据类型的基础上,提供了原子性的操作方法,保证了线程安全.以 ...
- Android自动化测试基础知识——MONKEY测试工具(转的)
本周开始启动手机输入法simeiji的自动化测试,同时开始接触手机浏览器自动化测试.接下来会对android自动化测试工具和方法做一个专题研究. 第一篇介绍monkey测试工具. 1 自动化测试背景 ...
- linux 工具: Top
linux TOP命令各参数详解[转载] http://www.cnblogs.com/sbaicl/articles/2752068.html
- BZOJ 1552: [Cerc2007]robotic sort( splay )
kpm大神说可以用块状链表写...但是我不会...写了个splay.... 先离散化 , 然后splay结点加个min维护最小值 , 就可以了... ( ps BZOJ 3506 题意一样 , 双倍经 ...
- Basic DataList
一,效果图. 二,源代码. <!DOCTYPE html><html><head> <meta charset="UTF-8"> & ...
- ORACLE的执行计划
转自:http://www.cnblogs.com/lovingprince/archive/2007/12/07/2166400.html 背景知识: 为了更好的进行下面的内容我们必须 ...
- Javascript 运动应用 02
JS运动应用 多个物体同时运动 例子:多个Div,鼠标移入 ...
- Markdown 11 种基础语法
现在是我在学习Markdown时做的笔记.学完这些Markdown的基本使用已经不成问题. 1. 标题设置(让字体变大,和word的标题意思一样)在Markdown当中设置标题,有两种方式: 第一种: ...
instantiateItem(View container, int position) {