调用 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自动更新后,我发现我的键盘突然就不管用了,检查了一下发现原来蓝牙没有打开,同时任务栏中的蓝牙图标也不见了. 不久之前,这样的情况已经出现过了一次,那次好像更新系统后就好了,但这次是系统更 ...
随机推荐
- 自己写的一个简单的Tab类
//------------- PS_DOM 功能函数 start----------------var PS_DOM ={ indexOf: function(arr, e){ for(var i= ...
- Qt configure 参数不完全说明
只需要加个 -fast参数就ok了.其他参数视自己情况而定,比如你不需要qt3支持可以添加-no-qt3support,或者不需要webkit插件 -no-webkit配置参数选项: 前面是*号的表示 ...
- oracle误删的表恢复
flashback table lip_organization to before drop;
- QQ邮箱添加公司邮箱步骤
经领导提示,发现QQ邮箱可以添加公司邮箱.这样,在有同事出差在外的时候,可以通过QQWEB邮箱,即可收发公司邮箱,不必安装邮箱客户端软件. 仅供各位同事参考使用. 1.打开QQ邮箱 2.点击其他邮箱, ...
- javascript 数组部分
<html> <body> <script type="text/javascript"> var arr = new Array(6) arr ...
- Android:AysncTask异步加载
以下是链接: http://blog.csdn.net/abc5382334/article/details/17097633 http://keeponmoving.iteye.com/blog/1 ...
- xzzx
创建包: CREATE OR REPLACE PACKAGE WYL_TEST_PKG_GGYW_XZZX IS -- Purpose : 公共业务-参保险种注销 --注销选择的险种,并将参保缴费信息 ...
- iOS 常用开源代码整理
本文章不定期整理. 1.AFNetworking AFNetworking 采用 NSURLConnection + NSOperation, 主要方便与服务端 API 进行数据交换, 操作简单, 功 ...
- Dispatcher & Redirect
首先理解一下二者的含义:Dispatcher请求转发,直接把客户端的请求在服务器处理以后跳转到下一个页面或者是处理类.此时的地址栏上的URL是不会变化的. Redirect是重定向.客户端的请求到达服 ...
- visual c++ 2010安装未成功
可能是已经安装了其他版本的Microsoft visual studio 参考: http://answers.microsoft.com/zh-hans/windows/forum/windows_ ...