简体   繁体   中英

How can I return fragments from Android PagerAdapter using the tab titles

I am trying to use android's view pager to dynamically load some fragments depending on the user profile type. But view-pager returns fragments based on position. An example describing my problem is below.

Example :

If user is free user only Profile and Settings tab are shown.But when an admin user is logged in he will have 4 extra tabs along with the above two. (The settings tab now is pushed to the last item.)

I tried to achieving this by storing the tab header in array-list and set the tab header based on the user type.By this I was able to show the tab headers correctly based on user type, but now the fragments are returned incorrectly.

So I was thinking whether I could return fragment based on the tab header.

How can I properly implement this?

PS : I have about 4 user types having different pages.

Code :

PagerAdapter:

@Override
public Fragment getItem(int position) {
    switch (position){
        case 0:
            return ProfileFragment.newInstance("","");
        case 1:
            return ManageOrdersFragment.newInstance("","");
        case 2:
            return ManageSellers.newInstance("","");
        case 3:
            return ProductsFragment.newInstance("","");
        case 4:
            return SettingsFragment.newInstance("","");
        default:
            return ProfileFragment.newInstance("","");
    }
}

TabHeaders:

General User : Profile , Settings

Seller : Profile,Products,Settings

Admin : Profile, Orders,Sellers,Settings

Problem:

Here the tab headers are correctly populated but the fragment returned for the 2nd tab is ManageOrders page for General User where it should be Settings. I understand that it takes the position of tabs to return the fragment. It would be really helpful if someone could help me fix this problem.

Thank you.

You just need to change what is returned based on user type using if or another switch

eg


@Override
public Fragment getItem(int position) {
  if ( getUserType().equals("admin"){
    switch (position){
        case 0:
            return ProfileFragment.newInstance("","");
        case 1:
            return ManageOrdersFragment.newInstance("","");
        case 2:
            return ManageSellers.newInstance("","");
        case 3:
            return SettingsFragment.newInstance("","");
        default:
            return ProfileFragment.newInstance("","");
    }
  } else if (getUserType().equals("seller"){
     switch (position){
        case 0:
            return ProfileFragment.newInstance("","");
        case 1:
            return ProductsFragment.newInstance("","");
        case 2:
            return SettingsFragment.newInstance("","");
        default:
            return ProfileFragment.newInstance("","");
  } else {
     switch (position){
        case 0:
            return ProfileFragment.newInstance("","");
        case 1:
            return SettingsFragment.newInstance("","");
        default:
            return ProfileFragment.newInstance("","");
  }
}

making sure your getPageTitles and getCount match

eg


@Override
public CharSequence getPageTitle(int position) {
  if ( getUserType().equals("admin"){
    switch (position){
        case 0:
            return "Profile";
        case 1:
            return "Orders;
        case 2:
            return "Sellers;
        case 3:
            return "Settings";
        default:
            return "Profile";
    }
  } else if (getUserType().equals("seller"){
    switch (position){
        case 0:
            return "Profile";
        case 1:
            return "Products";
        case 2:
            return "Settings";
        default:
            return "Profile";
  } else {
     switch (position){
        case 0:
            return "Profile";
        case 1:
            return "Settings";
        default:
            return "Profile";
  }

@Override
public int getCount() {
  if ( getUserType().equals("admin"){
    return 4;
  } else if (getUserType().equals("seller"){
    return 3;
  } else {
    return 2;
  }
}

And in this case the method getUserType() returns a string to match your user type, but you probably already have that stored somehow, so adjust to match. If you don't have something easily accessible already you could adjust the constructor of you Adapter to take an additional paramater

eg

class ViewPagerAdapter extends FragmentStatePagerAdapter {
   private String userType;


        ViewPagerAdapter(FragmentManager fm, String userType) {
            super(fm);
            this.userType = userType;
        }
 ......

and then when creating the adapter

mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(),"admin");

While this is probably not the most efficient way to do it, it is simple to show, you could data structures to hold what user type has what tabs and there are probably more optimal ways of organising the conditionals.

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