调用 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自动更新后,我发现我的键盘突然就不管用了,检查了一下发现原来蓝牙没有打开,同时任务栏中的蓝牙图标也不见了. 不久之前,这样的情况已经出现过了一次,那次好像更新系统后就好了,但这次是系统更 ...
随机推荐
- java学习之二叉树的实现
二叉树是一种数据结构,每个节点都有两个子节点. 二叉树的遍历有三种方式, 先序遍历是 根节点,左子树,右子树: 中序遍历是 左子树,根节点,右子树: 后序遍历是 左子树,右子树,根节点: java实现 ...
- UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环
X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...
- SqlServer和Oracle中一些常用的sql语句10 特殊应用
--482, ORACLE / SQL SERVER --订购数量超过平均值的书籍 WITH Orders_Book AS ( SELECT Book_Name, SUM(Qty) Book_Qty ...
- Gradle 1.12 翻译——第十四章. 教程 - 杂七杂八
有关其它已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或訪问:http://gradledoc.qiniudn.com ...
- CentOS下mysql最大连接数设置 1040 too many connection
当最大连接数比較小时,可能会出现"1040 too many connection"错误. 能够通过改动配置文件来改动最大连接数,但我连配置文件在哪都不知道,应该怎么办呢? 首先须 ...
- Linux内核态抢占机制分析(转)
Linux内核态抢占机制分析 http://blog.sina.com.cn/s/blog_502c8cc401012pxj.html 摘 要]本文首先介绍非抢占式内核(Non-Preemptive ...
- gcc支持c99验证
gcc3.0以上的版本都是支持C99标准的, 但是编译程序的时候需要加上 -std=c9 才可以: 一下程序是验证gcc是否支持c99标准的: #include <stdio.h> ...
- [Swust OJ 1094]--中位数(巧用set,堆排序)
题目链接:http://acm.swust.edu.cn/problem/1094/ Time limit(ms): 1000 Memory limit(kb): 32768 中位数(又称中值,英 ...
- sizeof,一个其貌不扬的家伙(转)
sizeof,一个其貌不扬的家伙,引无数菜鸟竟折腰,小虾我当初也没少犯迷糊,秉着“辛苦我一个,幸福千万人”的伟大思想,我决定将其尽可能详细的总结一下.但当我总结的时候才发现,这个问题既可以简单,又可以 ...
- POJ 2449 求第K短路
第一道第K短路的题目 QAQ 拿裸的DIJKSTRA + 不断扩展的A* 给2000MS过了 题意:大意是 有N个station 要求从s点到t点 的第k短路 (不过我看题意说的好像是从t到s 可能是 ...
instantiateItem(View container, int position) {