Daniele Vitali
Daniele Vitali

Reputation: 3858

Content not updating for FragmentPagerAdapter

I am using FragmentPagerAdapter and I need to refresh the content after user action.

To do this, I am calling notifyDataSetChanged() on the adapter but nothing seems happen. I would like to avoid using FragmentStatePagerAdapter, since it contains just 3 pages.

Here is the override methods of the adapter:

@Override
public int getCount() {
    return 3;
}

@Override
public Fragment getItem(int position) {
    Fragment fragment;
    switch (position) {
        case PAGE_HOME:
            fragment = HomeFragment.newInstance();
            break;
        case PAGE_CATEGORIES:
            fragment = CategoriesFragment.newInstance();
            break;
        case PAGE_ACCOUNT:
            if (account == null) {
                fragment = AccountFragment.newInstance();
            } else {
                fragment = MyAccountFragment.newInstance();
            }
            break;
        default:
            fragment = null;
    }
    pageReferenceMap.put(position, fragment);
    return fragment;
}

@Override
public CharSequence getPageTitle(int position) {
    return null;
}

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    Fragment fragment = (Fragment) super.instantiateItem(container, position);
    pageReferenceMap.put(position, fragment);
    return fragment;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    super.destroyItem(container, position, object);
    pageReferenceMap.remove(position);
}

Thanks.

Upvotes: 0

Views: 435

Answers (2)

Daniele Vitali
Daniele Vitali

Reputation: 3858

I found out that I have to return a different id for that page position. So here is how I return a unique id based on the fragment I want to display and not based on the page position:

@Override
public long getItemId(int position) {
    long id;
    switch (position) {
        case HOME_PAGE_POSITION:
            //fall through
        case CATEGORIES_PAGE_POSITION:
            id = position;
            break;
        case ACCOUNT_PAGE_POSITION:
            if (account == null) {
                id = position;
            } else {
                id = position + PAGE_COUNT;
            }
            break;
        default:
            id = -1;
    }
    return id;
}

If you have more that 2 fragments per pages to display at a time, you can just return position+PAGE_COUNT*n. Pretty easy.

Hope this help.

Upvotes: 1

Abhishek
Abhishek

Reputation: 1345

notifyDataSetChanged method will send a call back to your adapter , and check for the getCount method following this call back the getItem method is called. Your content in the screen refresh only if the content reflect any change in the currently selected fragment.The point to note here is that This callback will not select your first fragment. It will stay in the currently selected fragment.

For example:-

The currently selected fragment is 2, and you have changed the contents of fragment number 1 and makes a notifyDataSetChanged callback, so in this case you will not see any change as fragment 2 is currently present in the screen.

Upvotes: 0

Related Questions