Akshay Sudhakaran
Akshay Sudhakaran

Reputation: 35

How to view only some of the Fragments in a ViewPager

I have a ViewPager, and some fragments. I can add all the fragments together into the ViewPager to view the whole. For example: I have fragments named as Apple, Orange, Banana, Mango, Grapes, Cherry: 6 tabs The user will be provided with an option to select any three of these fruits. So let the user have chosen Apple, Banana, and Grapes Now the ViewPager should only show the 3 tabs of Apple, Banana, and Grapes.

How can I do that? I can set whole fruits into multiple tabs. But according to the selection, the unwanted tabs need to be removed or wanted tabs only need to be added.

I have this code as the adapter:

public class FruitFragAdapter extends FragmentStatePagerAdapter
{
    public FruitFragAdapter(FragmentManager fm)
    {
        super(fm);
    }


    public Fragment getItem(int position)
    {
        switch (position)
        {
            case 0: return new AppleFragment();
            case 1: return new OrangeFragment();
            case 2: return new BananaFragment();
            case 3: return new MangoFragment();
            case 4: return new GrapesFragment();
            case 5: return new CherryFragment();
        }
        return null;
    }


    @Override
    public int getCount()
    {
        return 6;
    }


    public CharSequence getPageTitle(int position)
    {
        switch (position)
        {
            case 0: return "Apple";
            case 1: return "Orange";
            case 2: return "Banana";
            case 3: return "Mango";
            case 4: return "Grapes";
            case 5: return "Cherry";
            default: return null;
        }
    }
}

Upvotes: 0

Views: 58

Answers (1)

HarmenH
HarmenH

Reputation: 221

You could do something like this.

From the chosen fragments by the user create an IntArray like [0,3,4] or [3,4,2,1] etc..

Initialize your adapter with that IntArray of the chosen fragment.

In getItem return the Fragment that the user chose for that specific position. In the example I created a function getFragmentBySelectedPosition for this.

getCount returns the number of chosen fragments (lenght of the IntArray)

class FruitFragAdapter(fm: FragmentManager, private val selectedFragments: IntArray) : FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

    private val appleFragment by lazy {
        AppleFragment().newInstance()
    }

    private val orangeFragment by lazy {
        OrangeFragment().newInstance()
    }

    private val bananaFragment by lazy {
        BananaFragment().newInstance()
    }

    private val mangoFragment by lazy {
        MangoFragment().newInstance()
    }

    private val grapesFragment by lazy {
        GrapesFragment().newInstance()
    }

    private val cherryFragment by lazy {
        CherryFragment().newInstance()
    }

    private fun getFragmentBySelectedPosition(position: Int): Fragment {
        return when (position) {
            0 -> appleFragment
            1 -> orangeFragment
            2 -> bananaFragment
            3 -> mangoFragment
            4 -> grapesFragment
            5 -> cherryFragment
            else -> throw IllegalArgumentException("No fragment for: $position")
        }
    }

    override fun getItem(position: Int): Fragment {
        return getFragmentBySelectedPosition(selectedFragments[position])
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return when (selectedFragments[position]) {
            0 -> "Apple"
            1 -> "Orange"
            2 -> "Banana"
            3 -> "Mango"
            4 -> "Grapes"
            5 -> "Cherry"
            else -> ""
        }
    }

    override fun getCount(): Int {
        return selectedFragments.count()
    }
}

Upvotes: 1

Related Questions