onTabSelected FragmentTransaction显示未显示

时间:2015-01-30 23:53:39

标签: android android-actionbar android-viewpager

因此,当我第一次启动我的活动时,代码将填充每个标签,actionBar.addTab(tab,true)将调用TabSelected,然后会添加每个片段。当我实际点击一个标签时,它应该只是调用fragmentTransaction.Show()并显示该片段。我只是空白。任何的想法?

以下是onCreate代码:

// missing some @override stuff etc.

public void onCreate {
    mSectionsPagerAdapter = new SectionsPagerAdapter(this.getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
        View tabView = inflater.inflate(R.layout.tab_title, null);

        TextView titleTV = (TextView) tabView.findViewById(R.id.action_custom_title);
        titleTV.setText(mSectionsPagerAdapter.getPageTitle(i));
        titleTV.setSingleLine();
        titleTV.setTextSize(13);
        Tab t = actionBar.newTab()
            .setText(mSectionsPagerAdapter.getPageTitle(i))
            .setTabListener(this);
        t.setCustomView(tabView);
        actionBar.addTab(t, true);
    }
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    Bundle b = new Bundle();

    switch (tab.getPosition()){
    case 0:
        b.putString("show", getString(R.string.title_section1));
        if (mShowFragment1 == null) {
            mShowFragment1 = EpisodeTileFragment.instantiate(this, EpisodeTileFragment.class.getName());
            mShowFragment1.setArguments(b);
            ft.add(android.R.id.content, mShowFragment1);
            addFragmentToActiveList(mShowFragment1);
        } else {
            ft.show(mShowFragment1);
        }           
        ft.show(mShowFragment1);
        break;
    case 1:
        b.putString("show", getString(R.string.title_section4));
        if(mShowFragment3 == null) {
            mShowFragment3 = EpisodeTileFragment.newInstance(getString(R.string.title_section4));
            ft.add(android.R.id.content, mShowFragment3);
        } 
        mShowFragment3.getArguments().putAll(b);
        ft.show(mShowFragment3);
        break;
    }
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        if(position == 0 || position == 1) {
            return EpisodeTileFragment.newInstance(getString(R.string.title_section1));
        } else  {
            return PlaceholderFragment.newInstance(position + 1);
        }
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        return "test";
    }
}

1 个答案:

答案 0 :(得分:0)

你正在调用错误的方法。 FragmentTransaction.show仅适用于已经附加且只是隐藏的片段。

您正在寻找的是fragmentTransaction.commit()

同时,您可能需要replace()当前显示的片段,而不是向现有容器添加新片段。

所以,总结一下:

ft.replace(id, fragment).commit();