简体   繁体   English

如何使用选项卡标题从 Android PagerAdapter 返回片段

[英]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.我正在尝试使用 android 的视图寻呼机根据用户配置文件类型动态加载一些片段。 But view-pager returns fragments based on position.但是 view-pager 根据位置返回片段。 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.如果用户是免费用户,则只显示“配置文件”和“设置”选项卡。但是当管理员用户登录时,他将有 4 个额外的选项卡以及上述两个选项卡。 (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. PS:我有大约 4 种具有不同页面的用户类型。

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.这里选项卡标题已正确填充,但为第二个选项卡返回的片段是一般用户的 ManageOrders 页面,它应该是设置。 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您只需要使用if或其他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确保您的getPageTitlesgetCount匹配

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.在这种情况下,方法getUserType()返回一个字符串以匹配您的用户类型,但您可能已经以某种方式存储了该字符串,因此请调整以匹配。 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.虽然这可能不是最有效的方法,但很容易展示,您可以使用数据结构来保存用户类型具有哪些选项卡,并且可能有更优化的组织条件的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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