@Config(sdk = Build.VERSION_CODES.JELLY_BEAN) @Test public void testSetPrimaryItem() { final FragmentManager mockFragmentManager = mock(FragmentManager.class); final FragmentPagerAdapter adapter = new Adapter(mockFragmentManager); final Fragment mockFragment1 = mock(Fragment.class); when(mockFragment1.getFragmentManager()).thenReturn(mockFragmentManager); adapter.setPrimaryItem(container, 0, mockFragment1); assertThat(adapter.getPrimaryFragment(), is(mockFragment1)); assertThat(adapter.getPrimaryPosition(), is(0)); verify(mockFragment1, times(1)).setMenuVisibility(true); verify(mockFragment1, times(1)).setUserVisibleHint(true); final Fragment mockFragment2 = mock(Fragment.class); when(mockFragment2.getFragmentManager()).thenReturn(mockFragmentManager); adapter.setPrimaryItem(container, 1, mockFragment2); assertThat(adapter.getPrimaryFragment(), is(mockFragment2)); assertThat(adapter.getPrimaryPosition(), is(1)); verify(mockFragment2).setMenuVisibility(true); verify(mockFragment2).setUserVisibleHint(true); verify(mockFragment1).setMenuVisibility(false); verify(mockFragment1).setUserVisibleHint(false); }
@Override public void setPrimaryItem(@NonNull final ViewGroup container, final int position, @Nullable final Object object) { final Fragment fragment = (Fragment) object; if (primaryItem != fragment) { this.primaryPosition = position; if (primaryItem != null) { this.primaryItem.setMenuVisibility(false); PagerAdapterHelper.setUserVisibleHint(primaryItem, false); } if (fragment != null) { fragment.setMenuVisibility(true); PagerAdapterHelper.setUserVisibleHint(fragment, true); } this.primaryItem = fragment; } }
FragmentPagerAdapter extends PagerAdapter { @Override public void setPrimaryItem(@NonNull final ViewGroup container, final int position, @Nullable final Object object) { final Fragment fragment = (Fragment) object; if (primaryItem != fragment) { this.primaryPosition = position; if (primaryItem != null) { this.primaryItem.setMenuVisibility(false); PagerAdapterHelper.setUserVisibleHint(primaryItem, false); } if (fragment != null) { fragment.setMenuVisibility(true); PagerAdapterHelper.setUserVisibleHint(fragment, true); } this.primaryItem = fragment; } } }
FragmentPagerAdapter extends PagerAdapter { @Override public void setPrimaryItem(@NonNull final ViewGroup container, final int position, @Nullable final Object object) { final Fragment fragment = (Fragment) object; if (primaryItem != fragment) { this.primaryPosition = position; if (primaryItem != null) { this.primaryItem.setMenuVisibility(false); PagerAdapterHelper.setUserVisibleHint(primaryItem, false); } if (fragment != null) { fragment.setMenuVisibility(true); PagerAdapterHelper.setUserVisibleHint(fragment, true); } this.primaryItem = fragment; } } FragmentPagerAdapter(@NonNull final FragmentManager fragmentManager); }
FragmentPagerAdapter extends PagerAdapter { @Override public void setPrimaryItem(@NonNull final ViewGroup container, final int position, @Nullable final Object object) { final Fragment fragment = (Fragment) object; if (primaryItem != fragment) { this.primaryPosition = position; if (primaryItem != null) { this.primaryItem.setMenuVisibility(false); PagerAdapterHelper.setUserVisibleHint(primaryItem, false); } if (fragment != null) { fragment.setMenuVisibility(true); PagerAdapterHelper.setUserVisibleHint(fragment, true); } this.primaryItem = fragment; } } FragmentPagerAdapter(@NonNull final FragmentManager fragmentManager); @Override void startUpdate(@NonNull final ViewGroup container); @SuppressLint("CommitTransaction") @Override @NonNull Object instantiateItem(@NonNull final ViewGroup container, final int position); long getItemId(final int position); @NonNull abstract Fragment getItem(int position); @Override void setPrimaryItem(@NonNull final ViewGroup container, final int position, @Nullable final Object object); @Nullable Fragment getPrimaryFragment(); @IntRange(from = NO_POSITION) int getPrimaryPosition(); @Override boolean isViewFromObject(@NonNull final View view, @NonNull final Object object); @SuppressLint("CommitTransaction") @Override void destroyItem(@NonNull final ViewGroup container, final int position, @NonNull final Object object); @Override void finishUpdate(@NonNull final ViewGroup container); }
FragmentPagerAdapter extends PagerAdapter { @Override public void setPrimaryItem(@NonNull final ViewGroup container, final int position, @Nullable final Object object) { final Fragment fragment = (Fragment) object; if (primaryItem != fragment) { this.primaryPosition = position; if (primaryItem != null) { this.primaryItem.setMenuVisibility(false); PagerAdapterHelper.setUserVisibleHint(primaryItem, false); } if (fragment != null) { fragment.setMenuVisibility(true); PagerAdapterHelper.setUserVisibleHint(fragment, true); } this.primaryItem = fragment; } } FragmentPagerAdapter(@NonNull final FragmentManager fragmentManager); @Override void startUpdate(@NonNull final ViewGroup container); @SuppressLint("CommitTransaction") @Override @NonNull Object instantiateItem(@NonNull final ViewGroup container, final int position); long getItemId(final int position); @NonNull abstract Fragment getItem(int position); @Override void setPrimaryItem(@NonNull final ViewGroup container, final int position, @Nullable final Object object); @Nullable Fragment getPrimaryFragment(); @IntRange(from = NO_POSITION) int getPrimaryPosition(); @Override boolean isViewFromObject(@NonNull final View view, @NonNull final Object object); @SuppressLint("CommitTransaction") @Override void destroyItem(@NonNull final ViewGroup container, final int position, @NonNull final Object object); @Override void finishUpdate(@NonNull final ViewGroup container); static final int NO_POSITION; static final long NO_ID; }