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 Object instantiateItem(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之我的解决方法的更多相关文章

  1. 【转】为什么调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment?

    为什么调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment? 转自:http://www.apkbus.com/android- ...

  2. 为什么调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment?

    在一个 Android 应用中,我使用 FragmentPagerAdapter 来 处理多 Fragment 页面的横向滑动.不过我碰到了一个问题,即当 Fragment 对应的数据集发生改变时,我 ...

  3. 为什么调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment?【转载】

    转载自:http://www.cnblogs.com/dancefire/archive/2013/01/02/why-notifyDataSetChanged-does-not-work.html ...

  4. 为什么调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment

    http://stackoverflow.com/questions/10849552/update-viewpager-dynamically if you want to switch out t ...

  5. mssql sqlserver 表增加列后,视图不会自动更新相关列的两种解决方法分享

    摘要: 今天对物理数据表,进行增加列操作后,程序一直显示无法找到相应列,通过仔细比对发现,视图中无相应列更新,下文将具体的解决方法分享如下: 例: create view vw_test as sel ...

  6. Win8.1无法安装更新,提示0x800*****错误的解决方法

    Win8.1无法安装更新,提示0x800*****错误的解决方法   注:本教程同样适用于Win10系统 有时候Win8.1某个系统文件的损坏会导致无法安装Windows更新,表现为Windows更新 ...

  7. 记前些日子archlinux更新后无法调节声音的解决方法

    桌面环境用的是xfce4. 自从某次更新过后,panel中调节声音的插件变成了 xfce4-pulseaudio-plugin.然后就发现在panel中无法调节声音了. 在这个插件的属性中发现了一项设 ...

  8. 【我的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 ...

  9. Win10更新后蓝牙出现故障的解决方法

    昨天Win10自动更新后,我发现我的键盘突然就不管用了,检查了一下发现原来蓝牙没有打开,同时任务栏中的蓝牙图标也不见了. 不久之前,这样的情况已经出现过了一次,那次好像更新系统后就好了,但这次是系统更 ...

随机推荐

  1. 转:触屏中的js事件

    一.触摸事件 ontouchstartontouchmoveontouchendontouchcancel 目前移动端浏览器均支持这4个触摸事件,包括IE.由于触屏也支持MouseEvent,因此他们 ...

  2. Zookper

    http://blog.csdn.net/yangbutao/article/details/11669609 http://www.oschina.net/code/snippet_255033_2 ...

  3. Windows Phone 8初学者开发—第5部分:布局和事件基础

    原文 Windows Phone 8初学者开发—第5部分:布局和事件基础 原文地址: http://channel9.msdn.com/Series/Windows-Phone-8-Developme ...

  4. 谷歌浏览器javascript调试教程

    扫扫关注"茶爸爸"微信公众号 坚持最初的执着,从不曾有半点懈怠,为优秀而努力,为证明自己而活. 回复:"茶爸爸"       看看有什么!! 怎样打开Chrom ...

  5. python urllib和urllib2 区别

    python有一个基础的库叫httplib.httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现 ...

  6. CloudStack 物理网络架构

    原文地址:http://www.shapeblue.com/cloudstack/understanding-cloudstacks-physical-networking-architecture/ ...

  7. MFC基础,MFC自绘控件学习总结.---转

    前言:从这学期开始就一直在学习自绘控件(mfc),目标是做出一款播放器界面,主要是为了打好基础,因为我基础实在是很烂....说说我自己心得体会以及自绘控件的方法吧,算是吐槽吧,说的不对和不全的地方,或 ...

  8. 统计单词频率--map

    问题描述: 输入一个单词列表,每行一个单词,统计单词出现的频率 思路: 主要是使用c++中的map容器.map实质上是一个二叉查找树,可以做到插入.删除.查询,平均查询时间在O(logn).n为map ...

  9. 典型c库函数的实现

    StrToInt:字符串转int输出 enum Status { kValid = , kInvalid = , }; int StrToInt(const char* str) { g_nStatu ...

  10. BZOJ 2718: [Violet 4]毕业旅行( 最长反链 )

    一不小心速度就成了#1.... 这道题显然是求最长反链, 最长反链=最小链覆盖.最小链覆盖就是先做一次floyd传递闭包, 再求最小路径覆盖. 最小路径覆盖=N - 二分图最大匹配. 所以把所有点拆成 ...