update notify on dataset change

This commit is contained in:
Ritvik Saraf 2018-10-02 21:30:11 +05:30
parent b8865e925d
commit cf3e53eb71
2 changed files with 19 additions and 7 deletions

View File

@ -44,27 +44,20 @@ public class TabAdaptor extends FragmentPagerAdapter {
public void addFragment(Fragment fragment, String title) { public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment); mFragmentList.add(fragment);
mFragmentTitleList.add(title); mFragmentTitleList.add(title);
notifyDataSetChanged();
} }
public void clearAllItems() { public void clearAllItems() {
mFragmentList.clear(); mFragmentList.clear();
mFragmentTitleList.clear(); mFragmentTitleList.clear();
notifyDataSetChanged();
} }
public void removeItem(int position){ public void removeItem(int position){
mFragmentList.remove(position == 0 ? 0 : position - 1); mFragmentList.remove(position == 0 ? 0 : position - 1);
mFragmentTitleList.remove(position == 0 ? 0 : position - 1); mFragmentTitleList.remove(position == 0 ? 0 : position - 1);
notifyDataSetChanged();
} }
public void updateItem(int position, Fragment fragment){ public void updateItem(int position, Fragment fragment){
mFragmentList.set(position, fragment); mFragmentList.set(position, fragment);
// shift the ID returned by getItemId outside the range of all previous fragments
// https://stackoverflow.com/questions/10396321/remove-fragment-page-from-viewpager-in-android
baseId += getCount() + 1;
notifyDataSetChanged();
} }
public void updateItem(String title, Fragment fragment){ public void updateItem(String title, Fragment fragment){
@ -79,4 +72,20 @@ public class TabAdaptor extends FragmentPagerAdapter {
if (mFragmentList.contains(object)) return mFragmentList.indexOf(object); if (mFragmentList.contains(object)) return mFragmentList.indexOf(object);
else return POSITION_NONE; else return POSITION_NONE;
} }
/**
* Notify that the position of a fragment has been changed.
* Create a new ID for each position to force recreation of the fragment
* @param n number of items which have been changed
*/
public void notifyChangeInPosition(int n) {
// shift the ID returned by getItemId outside the range of all previous fragments
// https://stackoverflow.com/questions/10396321/remove-fragment-page-from-viewpager-in-android
baseId += getCount() + n;
}
public void notifyDataSetUpdate(){
notifyChangeInPosition(1);
notifyDataSetChanged();
}
} }

View File

@ -819,6 +819,8 @@ public class VideoDetailFragment
pageAdapter.addFragment(new Fragment(), RELATED_TAB_TAG); pageAdapter.addFragment(new Fragment(), RELATED_TAB_TAG);
} }
pageAdapter.notifyDataSetUpdate();
if(pageAdapter.getCount() < 2){ if(pageAdapter.getCount() < 2){
tabLayout.setVisibility(View.GONE); tabLayout.setVisibility(View.GONE);
}else{ }else{
@ -1083,6 +1085,7 @@ public class VideoDetailFragment
if(showRelatedStreams){ if(showRelatedStreams){
pageAdapter.updateItem(RELATED_TAB_TAG, RelatedVideosFragment.getInstance(currentInfo)); pageAdapter.updateItem(RELATED_TAB_TAG, RelatedVideosFragment.getInstance(currentInfo));
pageAdapter.notifyDataSetUpdate();
} }
pushToStack(serviceId, url, name); pushToStack(serviceId, url, name);