Okay, the problem is almost the same with this post, but the solution there didn't solve my problem.

I use FragmentPagerAdapter with a List holding the Fragments like the below shown.

public class ViewPageAdapter extends FragmentPagerAdapter {
    private List<Fragment> fragments;

    public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    ...
}

And my Fragment overrides the onCreateView method like this.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
    return inflater.inflate(theLayoutResourceId, container, false);
}

If using the method in this post (override int getItemPosition(Object object) to return POSITION_NONE), I'll get java.util.ConcurrentModificationException after calling notifyDataSetChanged().

The method call flow is like this.

  1. at ViewPager page index 1, open a Dialog via clicking a Button (just the same as the LoginButton in the Facebook Android example)
  2. after the dialog closed (onAuthSucceed), remove ViewPager page index 1 and and add another page to be page index 1
  3. force update using notifyDataSetChanged

I've tried many other ways to remove a Fragment before adding a Fragment to the adapter, but the page will not update to the new content.

I just found one method to make the page blank...

viewpager.removeViewAt(1);
viewpagerAdapter.removeItem(1);
viewpagerAdapter.addItem(1, new Fragment(...));

Either solving the updating problem or java.util.ConcurrentModificationException would be great. Thanks for your help :)

有帮助吗?

解决方案

This post is kind of old, but in case anyone runs into this. I found the answer in a similar post here. It looks like you are on the right track but you need to change your FragmentPagerAdapter to a FragmentStatePagerAdapter and override the getItemPosition(Object), as you suggested in your post.

Best of luck to you.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top