From cf3e53eb71c5192a221e39ae07612427de617b77 Mon Sep 17 00:00:00 2001 From: Ritvik Saraf <13ritvik@gmail.com> Date: Tue, 2 Oct 2018 21:30:11 +0530 Subject: [PATCH] update notify on dataset change --- .../newpipe/fragments/detail/TabAdaptor.java | 23 +++++++++++++------ .../fragments/detail/VideoDetailFragment.java | 3 +++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/TabAdaptor.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/TabAdaptor.java index c091f45b2..2dd7071be 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/TabAdaptor.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/TabAdaptor.java @@ -44,27 +44,20 @@ public class TabAdaptor extends FragmentPagerAdapter { public void addFragment(Fragment fragment, String title) { mFragmentList.add(fragment); mFragmentTitleList.add(title); - notifyDataSetChanged(); } public void clearAllItems() { mFragmentList.clear(); mFragmentTitleList.clear(); - notifyDataSetChanged(); } public void removeItem(int position){ mFragmentList.remove(position == 0 ? 0 : position - 1); mFragmentTitleList.remove(position == 0 ? 0 : position - 1); - notifyDataSetChanged(); } public void updateItem(int position, Fragment 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){ @@ -79,4 +72,20 @@ public class TabAdaptor extends FragmentPagerAdapter { if (mFragmentList.contains(object)) return mFragmentList.indexOf(object); 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(); + } } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index 67f86a905..76047b725 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -819,6 +819,8 @@ public class VideoDetailFragment pageAdapter.addFragment(new Fragment(), RELATED_TAB_TAG); } + pageAdapter.notifyDataSetUpdate(); + if(pageAdapter.getCount() < 2){ tabLayout.setVisibility(View.GONE); }else{ @@ -1083,6 +1085,7 @@ public class VideoDetailFragment if(showRelatedStreams){ pageAdapter.updateItem(RELATED_TAB_TAG, RelatedVideosFragment.getInstance(currentInfo)); + pageAdapter.notifyDataSetUpdate(); } pushToStack(serviceId, url, name);