@Override public Object instantiateItem(ViewGroup container, int position) { mViewPager = ((ViewPager) container); int realPosition = (mAdapter instanceof FragmentPagerAdapter || mAdapter instanceof FragmentStatePagerAdapter) ? position : toRealPosition(position); if (mBoundaryCaching) { ToDestroy toDestroy = mToDestroy.get(position); if (toDestroy != null) { mToDestroy.remove(position); return toDestroy.object; } } if(realPosition >= 0 && realPosition < getRealCount()) return mAdapter.instantiateItem(container, realPosition); return null; }
private void initPager() { pager.setVisibility(View.VISIBLE); recyclerView.setVisibility(View.GONE); recyclerView.setAdapter(null); adapter = new FragmentStatePagerAdapter(getChildFragmentManager()) { @Override public int getCount() { return playlistcount; } @Override public Fragment getItem(int position) { return PlaylistPagerFragment.newInstance(position); } }; pager.setAdapter(adapter); pager.setOffscreenPageLimit(3); }
private void setupViews(PhotoViewPager viewPager) { final ArrayList<String> images = getIntent().getStringArrayListExtra(EXTRA_IMAGES); if (images == null || images.isEmpty()) { Log.e(TAG, "images is null "); // TODO: 2017/11/15 show a error view return; } FragmentStatePagerAdapter adapter = new FragmentStatePagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int i) { final String image = images.get(i); return PhotoViewFragment.newInstance(image); } @Override public int getCount() { return images.size(); } }; final int position = getIntent().getIntExtra(EXTRA_POSITION, 0); viewPager.setAdapter(adapter); viewPager.setCurrentItem((position >= images.size() || position <= 0) ? 0 : position); }
/** * 将back事件分发给ViewPager中的Fragment,{@link #handleBackPress(FragmentManager)} 已经实现了对ViewPager的支持,所以自行决定是否使用该方法 * * @return 如果处理了back键则返回 <b>true</b> * @see #handleBackPress(FragmentManager) * @see #handleBackPress(Fragment) * @see #handleBackPress(FragmentActivity) */ public static boolean handleBackPress(ViewPager viewPager) { if (viewPager == null) return false; PagerAdapter adapter = viewPager.getAdapter(); if (adapter == null) return false; int currentItem = viewPager.getCurrentItem(); Fragment fragment; if (adapter instanceof FragmentPagerAdapter) { fragment = ((FragmentPagerAdapter) adapter).getItem(currentItem); } else if (adapter instanceof FragmentStatePagerAdapter) { fragment = ((FragmentStatePagerAdapter) adapter).getItem(currentItem); } else { fragment = null; } return isFragmentBackHandled(fragment); }
private void checkFragmentStatus(final Activity activity, final int adapterItemIndex) { final ViewPager viewPager = (ViewPager) activity.findViewById(R.id.pager); assertNotNull(viewPager); final FragmentStatePagerAdapter adapter = (FragmentStatePagerAdapter)viewPager.getAdapter(); assertNotNull(adapter); Fragment fragment = adapter.getItem(adapterItemIndex); assertNotNull(fragment); Bundle arguments = fragment.getArguments(); assertNotNull(arguments); final int expectedTransactionType = (adapterItemIndex == 0) ? DBHelper.TransactionDbIds.EXPENSE : DBHelper.TransactionDbIds.REVENUE; assertEquals(expectedTransactionType, arguments.getInt("type")); }
@Test public void correctFragmentsInTabs() { ActivityController activityController = Robolectric.buildActivity(TransactionActivity.class).create(); Activity activity = (Activity)activityController.get(); activityController.start(); activityController.resume(); final ViewPager viewPager = (ViewPager) activity.findViewById(R.id.pager); assertNotNull(viewPager); final FragmentStatePagerAdapter adapter = (FragmentStatePagerAdapter)viewPager.getAdapter(); assertNotNull(adapter); assertEquals(2, adapter.getCount()); checkFragmentStatus(activity, 0); checkFragmentStatus(activity, 1); }
private void initFragmentAdapter() { frameLayout=(FrameLayout) findViewById(R.id.fragHome); statePagerAdapter=new FragmentStatePagerAdapter(getSupportFragmentManager()) { public int getCount() { return 4; } public Fragment getItem(int arg0) { switch (arg0) { case 0: return new NewsFragment(); case 1: return new HotgoodsFragment(); case 2: return new ITcircleFragment(); case 3: return new MineFragment(); default: return null; } } }; }
@Override protected void refreshFromOptionsMenu() { super.refreshFromOptionsMenu(); /** this hack worked with FragmenPagerAdapter but not with FragmentStatePagerAdapter */ // Fragment currentFragment = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pegel_data_pager + ":" + mPager.getCurrentItem()); Fragment currentFragment = (Fragment) ((FragmentStatePagerAdapter)mPagerAdapter).instantiateItem(mPager, mPager.getCurrentItem()); if(currentFragment != null ) { switch (mPager.getCurrentItem()) { case 0: ((PegelDataFragment) currentFragment).loadData(true); break; case 1: ((PegelDataDetailFragment) currentFragment).loadData(true); break; case 2: ((PegelDetailFragment) currentFragment).loadData(true); break; case 3: ((PegelMapFragment) currentFragment).loadData(true); break; } } }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment // create ContextThemeWrapper from the original Activity Context with the custom theme final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.PagerIndicatorDefaultNewWithDivider); // clone the inflater using the ContextThemeWrapper LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper); // Inflate the layout for this fragment View rootView = localInflater.inflate(R.layout.fragment_card_payment, container, false); FragmentStatePagerAdapter adapter = new SavePayAdapter(getChildFragmentManager()); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); pager.setOffscreenPageLimit(options.size()); pager.setAdapter(adapter); TabPageIndicator indicator = (TabPageIndicator) rootView.findViewById(R.id.indicator); indicator.setViewPager(pager); return rootView; }
private void initViews() { mViewPager.setAdapter(new FragmentStatePagerAdapter( getSupportFragmentManager()) { @Override public int getCount() { return VolleyUtil.mImages.size(); } @Override public Fragment getItem(int position) { return BeautyFragment.newInstance(VolleyUtil.mImages.get(position) .getUrl()); } }); int positon = getIntent().getIntExtra(POSITION, 0); mViewPager.setCurrentItem(positon); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pager); final MultiViewPager pager = (MultiViewPager) findViewById(R.id.pager); final FragmentStatePagerAdapter adapter = new FragmentStatePagerAdapter(getSupportFragmentManager()) { @Override public int getCount() { return 20; } @Override public Fragment getItem(int position) { return PageFragment.create(position); } }; pager.setAdapter(adapter); }
public void addTabs(FragmentStatePagerAdapter pageAdapter, final ViewPager pager, boolean scrollable) { if (tab_showed) { return; } final AppBarLayout appBar = (AppBarLayout) findViewById(R.id.appbar); final TabLayout tabLayout = (TabLayout) getLayoutInflater().inflate(R.layout.tab_layout, null); tabLayout.post(new Runnable() { @Override public void run() { tabLayout.setupWithViewPager(pager); } }); if (!scrollable) { tabLayout.setTabMode(TabLayout.MODE_FIXED); } appBar.addView(tabLayout, 1); pager.setAdapter(pageAdapter); tab_showed = true; }
private void test1() { setContentView(R.layout.activity_test_2); BackgroundViewPager2 viewPager = (BackgroundViewPager2) findViewById(R.id.viewPager2); viewPager.setViewPagerBackground(R.drawable.wide_bg); viewPager.setAdapter(new FragmentStatePagerAdapter( getSupportFragmentManager()) { @Override public int getCount() { return 3; } @Override public Fragment getItem(int arg0) { return MyFragment.newInstance(arg0 + 1); } }); }
private void test2() { setContentView(R.layout.activity_test); BackgroundViewPager viewPager = (BackgroundViewPager) findViewById(R.id.flowViewPager1); viewPager.setAdapter(new FragmentStatePagerAdapter( getSupportFragmentManager()) { @Override public int getCount() { return 3; } @Override public Fragment getItem(int arg0) { return MyFragment.newInstance(arg0 + 1); } }); }
@Before public void setUp() throws IllegalAccessException { context = Robolectric.application; initMocks(this); wizardActivity = new WizardActivity() { FragmentStatePagerAdapter getWizardPagerAdapter() { return mockPagerAdapter; } }; // wizardActivity.onCreate(null); // // when(mockFragment.action()).thenReturn("Action"); // when(mockFragment.performAction()).thenReturn(true); // when(mockFragment.getView()).thenReturn(mock(EditText.class)); // when(mockPagerAdapter.getItem(anyInt())).thenReturn(mockFragment); // when(mockPagerAdapter.getCount()).thenReturn(3); // previousButton = (Button) wizardActivity.findViewById(R.id.previous_button); // actionButton = (Button) wizardActivity.findViewById(R.id.action_button); // viewPager = (WizardViewPager) wizardActivity.findViewById(R.id.wizard_view_pager); }
@Override public Object instantiateItem(ViewGroup container, int position) { int realPosition = (mAdapter instanceof FragmentPagerAdapter || mAdapter instanceof FragmentStatePagerAdapter) ? position : toRealPosition(position); if (mBoundaryCaching) { ToDestroy toDestroy = mToDestroy.get(position); if (toDestroy != null) { mToDestroy.remove(position); return toDestroy.object; } } return mAdapter.instantiateItem(container, realPosition); }
@Override public void destroyItem(ViewGroup container, int position, Object object) { int realFirst = getRealFirstPosition(); int realLast = getRealLastPosition(); int realPosition = (mAdapter instanceof FragmentPagerAdapter || mAdapter instanceof FragmentStatePagerAdapter) ? position : toRealPosition(position); if (mBoundaryCaching && (position == realFirst || position == realLast)) { mToDestroy.put(position, new ToDestroy(container, realPosition, object)); } else { mAdapter.destroyItem(container, realPosition, object); } }
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_crime_pager); UUID crimeId = (UUID) getIntent().getSerializableExtra(EXTRA_CRIME_ID); mViewPager = (ViewPager) findViewById(R.id.activity_crime_pager_view_pager); mCrimes = CrimeLab.get(this).getCrimes(); FragmentManager fragmentManager = getSupportFragmentManager(); mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) { @Override public Fragment getItem(int position) { Crime crime = mCrimes.get(position); return CrimeFragment.newInstance(crime.getId()); } @Override public int getCount() { return mCrimes.size(); } }); for (int i = 0; i < mCrimes.size(); i++){ if (mCrimes.get(i).getId().equals(crimeId)){ mViewPager.setCurrentItem(i); break; } } }
@Override public Object instantiateItem(ViewGroup container, int position) { int innerPosition = (mAdapter instanceof FragmentPagerAdapter || mAdapter instanceof FragmentStatePagerAdapter) ? position : toInnerPosition(position); if (mBoundaryCaching) { ToDestroy toDestroy = mToDestroy.get(position); if (toDestroy != null) { mToDestroy.remove(position); return toDestroy.object; } } return mAdapter.instantiateItem(container, innerPosition); }
@Override public void destroyItem(ViewGroup container, int position, Object object) { int innerPosition = (mAdapter instanceof FragmentPagerAdapter || mAdapter instanceof FragmentStatePagerAdapter) ? position : toInnerPosition(position); if (mBoundaryCaching && mToDestroy.get(innerPosition) == null) { mToDestroy.put(position, new ToDestroy(container, innerPosition, object)); } else { mAdapter.destroyItem(container, innerPosition, object); } }
@Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_style_selector, container, false); if (ACTION.equals(Constants.SETTINGS_STYLE_SELECTOR_NOWPLAYING)) { } pager = (MultiViewPager) rootView.findViewById(R.id.pager); adapter = new FragmentStatePagerAdapter(getChildFragmentManager()) { @Override public int getCount() { return 6; } @Override public Fragment getItem(int position) { selectorFragment = SubStyleSelectorFragment.newInstance(position, ACTION); return selectorFragment; } @Override public int getItemPosition(Object object) { return POSITION_NONE; } }; pager.setAdapter(adapter); scrollToCurrentStyle(); return rootView; }
@Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_news, container, false); toolbar = (Toolbar) view.findViewById(R.id.toolbar); toolbar.setTitle(getString(R.string.title_news)); tabLayout = (TabLayout) view.findViewById(R.id.tabLayout); viewPager = (ViewPager) view.findViewById(R.id.viewPager); tabLayout.addOnTabSelectedListener(this); viewPager.addOnPageChangeListener(this); viewPager.setAdapter(new FragmentStatePagerAdapter(getFragmentManager()) { @Override public Fragment getItem(int position) { switch (position) { case 0: return itNewsFragment; case 1: return otherNewsFragment; } return null; } @Override public int getCount() { return 2; } }); return view; }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = (ViewPager) findViewById(R.id.home_view_pager); viewPager.setOffscreenPageLimit(2); viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { switch (position) { case 0: return todoFragment; case 1: return newsFragment; case 2: return bookFragment; } return null; } @Override public int getCount() { return 3; } }); viewPager.addOnPageChangeListener(this); navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); runOnUiThread(new Runnable() { @Override public void run() { MainActivity.this.setTitle(getString(R.string.title_news)); } }); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_oschina); toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle(R.string.oschina); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { OSChinaActivity.this.finish(); } }); tabLayout = (TabLayout) findViewById(R.id.tabLayout); viewPager = (ViewPager) findViewById(R.id.viewPager); tabLayout.addOnTabSelectedListener(this); viewPager.addOnPageChangeListener(this); viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { switch (position) { case 0: return osChinaIndustryFragment; case 1: return osChinaProjectFragment; } return null; } @Override public int getCount() { return 2; } }); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_v2ex); toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle(R.string.v2ex); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { V2exActivity.this.finish(); } }); tabLayout = (TabLayout) findViewById(R.id.tabLayout); viewPager = (ViewPager) findViewById(R.id.viewPager); tabLayout.addOnTabSelectedListener(this); viewPager.addOnPageChangeListener(this); viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { switch (position) { case 0: return v2exLatestFragment; case 1: return v2exHotFragment; } return null; } @Override public int getCount() { return 2; } }); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_crime_pager); UUID crimeId = (UUID) getIntent() .getSerializableExtra(EXTRA_CRIME_ID); mViewPager = (ViewPager) findViewById(R.id.crime_view_pager); mCrimes = CrimeLab.get(this).getCrimes(); FragmentManager fragmentManager = getSupportFragmentManager(); mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) { @Override public Fragment getItem(int position) { Crime crime = mCrimes.get(position); return CrimeFragment.newInstance(crime.getId()); } @Override public int getCount() { return mCrimes.size(); } }); for (int i = 0; i < mCrimes.size(); i++) { if (mCrimes.get(i).getId().equals(crimeId)) { mViewPager.setCurrentItem(i); break; } } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_crime_pager); UUID crimeId = (UUID) getIntent() .getSerializableExtra(EXTRA_CRIME_ID); mViewPager = (ViewPager) findViewById(R.id.view_pager); mCrimes = CrimeLab.get(this).getCrimes(); FragmentManager fragmentManager = getSupportFragmentManager(); mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) { @Override public Fragment getItem(int position) { Crime crime = mCrimes.get(position); return CrimeFragment.newInstance(crime.getId()); } @Override public int getCount() { return mCrimes.size(); } }); for (int i = 0; i < mCrimes.size(); i++) { if (mCrimes.get(i).getId().equals(crimeId)) { mViewPager.setCurrentItem(i); break; } } }