android.support.v4.app.FragmentStatePagerAdapter Java Examples

The following examples show how to use android.support.v4.app.FragmentStatePagerAdapter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LoopPagerAdapterWrapper.java    From MVVM-JueJin with MIT License 6 votes vote down vote up
@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;
}
 
Example #2
Source File: StudyAct.java    From Alibaba-Android-Certification with MIT License 6 votes vote down vote up
@Override
public void showQuestionFragment(final ArrayList<BaseFragment> baseFragments) {
    this.mFragments=baseFragments;
    viewpager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            return baseFragments.get(position);
        }

        @Override
        public int getCount() {
            return baseFragments.size();
        }
    });

}
 
Example #3
Source File: PhotoViewerActivity.java    From AndroidUiKit with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #4
Source File: PagerActivity.java    From MultiViewPager with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #5
Source File: BackHandlerHelper.java    From FragmentBackHandler with Apache License 2.0 6 votes vote down vote up
/**
 * 将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);
}
 
Example #6
Source File: TransactionActivityTest.java    From budget-watch with GNU General Public License v3.0 6 votes vote down vote up
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"));
}
 
Example #7
Source File: TransactionActivityTest.java    From budget-watch with GNU General Public License v3.0 6 votes vote down vote up
@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);
}
 
Example #8
Source File: LoopPagerAdapterWrapper.java    From openshop.io-android with MIT License 6 votes vote down vote up
@NonNull
@Override
public Object instantiateItem(@NonNull 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);
}
 
Example #9
Source File: LoopPagerAdapterWrapper.java    From SpinningTabStrip with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #10
Source File: LoopPagerAdapterWrapper.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #11
Source File: LoopPagerAdapterWrapper.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #12
Source File: LoopPagerAdapterWrapper.java    From openshop.io-android with MIT License 5 votes vote down vote up
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull 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);
    }
}
 
Example #13
Source File: LoopPagerAdapterWrapper.java    From SuperIndicator with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #14
Source File: LoopPagerAdapterWrapper.java    From SuperIndicator with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #15
Source File: StyleSelectorFragment.java    From Muzesto with GNU General Public License v3.0 5 votes vote down vote up
@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 4;
        }

        @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);

    return rootView;
}
 
Example #16
Source File: LoopPagerAdapterWrapper.java    From SpinningTabStrip with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #17
Source File: LoopPagerAdapterWrapper.java    From FlycoBanner_Master with MIT License 5 votes vote down vote up
@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);
}
 
Example #18
Source File: LoopPagerAdapterWrapper.java    From FlycoBanner_Master with MIT License 5 votes vote down vote up
@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);
	}
}
 
Example #19
Source File: CalcActivity.java    From Equate with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Helper function to return the convert key fragment at position pos
 *
 * @param pos the position of the desired convert key fragment
 * @return will return the fragment or null if it doesn't exist at that position
 */
private ConvKeysFragment getConvKeyFrag(int pos) {
	FragmentStatePagerAdapter tempAdapter =
			  (FragmentStatePagerAdapter) mUnitTypeViewPager.getAdapter();
	//make sure we aren't trying to access an invalid page fragment
	if (pos < tempAdapter.getCount() && pos >= 0){
		return (ConvKeysFragment) tempAdapter.
				  instantiateItem(mUnitTypeViewPager, pos);
	} else return null;
}
 
Example #20
Source File: LoopPagerAdapterWrapper.java    From LoopingViewPager with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #21
Source File: LoopPagerAdapterWrapper.java    From LoopingViewPager with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #22
Source File: LoopPagerAdapterWrapper.java    From Android-Application-ZJB with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #23
Source File: LoopPagerAdapterWrapper.java    From Android-Application-ZJB with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #24
Source File: FolioPageFragmentAdapter.java    From ankihelper with GNU General Public License v3.0 5 votes vote down vote up
public ArrayList<Fragment.SavedState> getSavedStateList() {

        if (savedStateList == null) {
            try {
                Field field = FragmentStatePagerAdapter.class.getDeclaredField("mSavedState");
                field.setAccessible(true);
                savedStateList = (ArrayList<Fragment.SavedState>) field.get(this);
            } catch (Exception e) {
                Log.e(LOG_TAG, "-> ", e);
            }
        }

        return savedStateList;
    }
 
Example #25
Source File: CrimePagerActivity.java    From AndroidProgramming3e with Apache License 2.0 5 votes vote down vote up
@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;
        }
    }
}
 
Example #26
Source File: CrimePagerActivity.java    From AndroidProgramming3e with Apache License 2.0 5 votes vote down vote up
@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;
        }
    }
}
 
Example #27
Source File: CrimePagerActivity.java    From AndroidProgramming3e with Apache License 2.0 5 votes vote down vote up
@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;
        }
    }
}
 
Example #28
Source File: CrimePagerActivity.java    From AndroidProgramming3e with Apache License 2.0 5 votes vote down vote up
@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;
        }
    }
}
 
Example #29
Source File: CrimePagerActivity.java    From AndroidProgramming3e with Apache License 2.0 5 votes vote down vote up
@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;
        }
    }
}
 
Example #30
Source File: CrimePagerActivity.java    From AndroidProgramming3e with Apache License 2.0 5 votes vote down vote up
@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;
        }
    }
}