FragmentPagerAdapter 更新数据遇到了坑啊。下拉刷新不会更新页面、有缓存。

来源:互联网 发布:淘宝店铺客服电话 编辑:程序博客网 时间:2024/06/01 14:58

ViewPager+FragmentPagerAdapter:

更新Fragment里数据是不起作用,FragmentPagerAdapter添加或减少Fragment时,前面的Fragment内容更新不起作用等等问题,有的做法是暴力删除fragment列表

粗暴解决方案1:

List<Fragment> fragments = getSupportFragmentManager().getFragments();
for (int i = fragments.size() - 1; i >= 0; i--) {
getSupportFragmentManager().beginTransaction().remove(fragments.get(0));
}


粗暴解决方案2:

    @Overridepublic Object instantiateItem(ViewGroup container, int position) {    if (mCurTransaction == null) {        mCurTransaction = mFragmentManager.beginTransaction();    }    final long itemId = getItemId(position);    // Do we already have this fragment?    String name = makeFragmentName(container.getId(), itemId);    Fragment fragment = mFragmentManager.findFragmentByTag(name);    if (fragment != null) {        if (DEBUG) Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment);        mCurTransaction.attach(fragment);    } else {        fragment = getItem(position);        if (DEBUG) Log.v(TAG, "Adding item #" + itemId + ": f=" + fragment);        mCurTransaction.add(container.getId(), fragment,                makeFragmentName(container.getId(), itemId));    }    if (fragment != mCurrentPrimaryItem) {        fragment.setMenuVisibility(false);        fragment.setUserVisibleHint(false);    }    return fragment;} @Overridepublic long getItemId(int position) {    // 获取当前数据的hashCode    int hashCode = fragments.get(position).hashCode();    return hashCode;}

阅读全文
0 0