ViewPager PageAdapter not updating View
-
27-10-2019 - |
문제
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 Fragment
s 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.
- at
ViewPager
page index 1, open aDialog
via clicking aButton
(just the same as theLoginButton
in the Facebook Android example) - after the dialog closed (
onAuthSucceed
), removeViewPager
page index 1 and and add another page to be page index 1 - 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.