Reputation: 445
Been stuck on this for a while.
I currently have 3 fragments each containing lists - A | B | C (Favorites)
A and B retrieve data from online, C is an offline favorite list.
When a user favorites something in A it shows up in the favorite list straight away as the ViewPageAdapter loads 1 extra page off the screen. So A | B are already loaded, which means when I go to favorites (C) it has to reload.
My problem is - When I favorite something in B, the app refuses to reload favorites (C) as C was already loaded when I clicked on B and the only way to see what I have added is refresh the app.
I have tried:
Changing setOffscreenPageLimit(); to 0 so it has to reload each fragment every time - even if clunky just to see it working, and it still refuses to.
NotifyDataSetChanged also hasn't worked or I don't understand it properly.
InstatiateItem in the ViewPageAdapter, but couldn't get that working, couldn't find a good example of it to understand
Creating a new listadapter in the favorite code just to try to get it load the new data - which I can see in the logs it is adding to the favourite list but it just isn't being reloaded by the ViewPageAdapter
Lots of Googling
What happens is, regardless of what I do, when the user goes from B to C, no new code runs as C's code all ran once I clicked on B and it just goes straight to C's list.
I'm using Google's SlidingTabLayout and SlidingTabStrip for the fragments which all works fine, nothing changed in it. found here - https://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html
Code:
public void favourite()
{
//if the user wants to favourite
ParseQuery<ParseObject> query = ParseQuery.getQuery("Favourite");
//queries the id from the local data store in-case they have already favourited
query.whereEqualTo("id",id);
query.fromLocalDatastore();
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e)
{
if(object!=null)
{
Toast.makeText(getApplicationContext(),"You have already pinned this",Toast.LENGTH_SHORT).show();
}
else
{
//otherwise create a new ParseObject and save the id,title and backdrop to
//the local datastore
final ParseObject favouriteShow = new ParseObject("FavouriteShow");
favouriteShow.put("id", id);
favouriteShow.put("title", title);
favouriteShow.put("backdrop", backdropPath);
favouriteShow.pinInBackground();
Toast.makeText(getApplicationContext(),"Pinned - "+ title,Toast.LENGTH_SHORT).show();
//need to set favourite as null for it to redraw with the favourited icon
mFavourite.setImageDrawable(null);
mFavourite.setImageResource(R.drawable.favourited_already);
}
}
});
}
ViewPagerAdapter:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[];
int tabNumber;
public ViewPagerAdapter(FragmentManager fm, CharSequence[] mTitles, int mTabNumber) {
super(fm);
this.Titles = mTitles;
this.tabNumber = mTabNumber;
}
@Override
public Fragment getItem(final int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new FragmentA();
break;
case 1:
fragment = new FragmentB();
break;
case 2:
fragment = new FavouritesFragmentC();
break;
}
return fragment;
}
@Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
@Override
public int getCount() {
return tabNumber;
}
}
Thanks very much for your help.
Adam
Upvotes: 0
Views: 233
Reputation: 127
I've personally had a similar problem.
The solution to this problem depends on the whether the data shown in each tab is contextually related.
If not, the best way to fix this is actually changing the navigational structure of the application. So instead of having tabs, you should use a navigation drawer. Therefore every time the user enters the screen through the drawer, the fragment is created anew. By doing this it will solve the problem you have and improve the user experience.
If so, you may need to create as base fragment for all of the tabs to inherit, with a function called refresh, then you can ask the Adapter to refresh all your tabs. This method is quite hacky mostly because a favourites section doesn't belong in tabs.
Upvotes: 0
Reputation: 266
Use ViewPager.OnPageChangeListener and update the corresponding fragment in onPageSelected(position) method using callback pattern.
Upvotes: 1