基于用户模式

时间:2015-08-23 21:57:23

标签: android-fragments android-viewpager

我目前难以根据所选用户模式通过ViewPager显示不同的标签集。如果我没有更改用户模式,则制表符/片段/ ViewPager会按预期运行。如果我更改usermode(使用与ViewPager不同的适配器),则会显示正确数量的选项卡,但每个选项卡中的片段无法正确更新。下面是代码。提前感谢任何建议。

在Resume上使用所有魔法。 onCreate是正常的布局设置。

// -------------------------------------------------
@Override
public void onResume() {
    super.onResume();
    if(isActivityForResult){
        return;
    }

    if (PreferenceManager.getDefaultSharedPreferences(getActivity()).getString("userMode", "").equalsIgnoreCase("buyer")) {
        TextView title = (TextView) rootView.findViewById(R.id.mfm_main_header_title);
        title.setText(R.string.buyer_main_fragment_title);
        adapter = new BuyerSectionsPagerAdapter(getChildFragmentManager());

    } else if (PreferenceManager.getDefaultSharedPreferences(getActivity()).getString("userMode", "").equalsIgnoreCase("seller")) {
        TextView title = (TextView) rootView.findViewById(R.id.mfm_main_header_title);
        title.setText(R.string.seller_main_fragment_title);
        adapter = new SellerSectionsPagerAdapter(getChildFragmentManager());
    }

    setUpViewTabs(adapter);
    setNavigationDrawer();
}


// Set up tabs for either buyer or seller perspectives
// -------------------------------------------------
public void setUpViewTabs(PagerAdapter adapter) {

    // Initialize the ViewPager and set an adapter
    ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager);
    pager.setAdapter(adapter);

    // Bind the tabs to the ViewPager
    PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) rootView.findViewById(R.id.tabs);
    tabs.setTextColor(getResources().getColor(R.color.themeCommonBGColor));
    tabs.setIndicatorColor(getResources().getColor(R.color.themeYellow));
    tabs.setIndicatorHeight(4);
    tabs.setTabBackground(R.drawable.tab_background_statelist);
    tabs.setTabPaddingLeftRight(0);
    tabs.setAllCaps(true);
    tabs.setShouldExpand(true);
    tabs.setViewPager(pager);
}


// -------------------------------------------------
// -------------------------------------------------
public class BuyerSectionsPagerAdapter extends FragmentPagerAdapter {

    public BuyerSectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    // -------------------------------------------------
    @Override
    public Fragment getItem(int position) {

        switch (position) {

            case 0: {
                return BuyerMyFarmersRootFragment.newInstance();
            }
            case 1: {
                return BuyerMyOrdersFragment.newInstance();
            }

            default: {
                return null;
            }
        }
    }

    // -------------------------------------------------
    @Override
    public int getCount() {
        return 2;

    }

    // -------------------------------------------------
    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0: {
                return getString(R.string.buyer_fragment_my_farmers)
                        .toUpperCase(l);
            }
            case 1: {
                return getString(R.string.buyer_fragment_my_orders)
                        .toUpperCase(l);
            }

            default: {
                return null;
            }
        }

    }

}

// -------------------------------------------------
// -------------------------------------------------
public class SellerSectionsPagerAdapter extends FragmentPagerAdapter {

    // -------------------------------------------------
    public SellerSectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    // -------------------------------------------------
    @Override
    public Fragment getItem(int position) {

        switch (position) {

            case 0: {
                return SellerOrdersRootFragment.newInstance();
            }
            case 1: {
                return SellerProductsRootFragment.newInstance();
            }
            case 2: {
                return SellerCustomersRootFragment.newInstance();
            }
            default:
                return null;
        }

    }

    // -------------------------------------------------
    @Override
    public int getCount() {
        return 3;

    }

    // -------------------------------------------------
    @Override
    public CharSequence getPageTitle(int position) {

        switch (position) {
            case 0:
                return getString(R.string.seller_fragment_title_orders);
            case 1:
                return getString(R.string.seller_fragment_title_products);
            case 2:
                return getString(R.string.seller_fragment_title_customers);
        }
        return null;
    }

}

0 个答案:

没有答案