简体   繁体   中英

Replace fragment with another in ViewPager ( after authorization)

I have 3 (fragment1, fragment2, fragment3) fragments in viewpager. The fragment1 and the fragment2 have a recyclerview inside. The fragment3 is a simple login page. What is the right way to replace fragment3 with a new fragment4 after authorization? (the fragment4 has inside another recyclerview).

Option1:

I would suggest setting total pages as 3 in view pager, then in your class which extends FragmentStatePagerAdapter,

@Override
public Fragment getItem(int position) {
    if (position == 1 or 2) {
        //Fragment 1 or 2
    } else if (position== 3) {
        if (isAuthorized) {
            //Fragment 4
        } else {
            //Fragment 3
        }
    }
}

Option 2:

Keep Login and recycler UI part of Fragment 3 and toggle the UI as required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM