1

我有一个使用 TabBarSherlock 和 Support 库构建的应用程序,用于将 ActionBar 支持添加到 3.0 之前的设备。我不记得我遵循什么教程来创建选项卡和侦听器,但我有以下代码。

首先创建选项卡(在 SherlockFragmentActivity 内):

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

/*--------Setup News Tab--------*/
Tab tab1 = actionBar.newTab()
        .setText("News")
        .setTabListener(new TabListener<TabFragment>(
                        this, "tab1", TabFragment.class));
Bundle newsBundle = new Bundle();
newsBundle.putInt("news_id", newsID);
tab1.setTag(newsBundle);
actionBar.addTab(tab1);
/*------------------------------*/
// This is repeated 3 more times to total 4 Tabs.

然后我有一个 classCalled TabListener,在每个选项卡中使用它来检测它们何时被选中。

public class TabListener<T extends Fragment> implements ActionBar.TabListener{
    private TabFragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    public TabListener(Activity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // Check if the fragment is already initialised
        if (mFragment == null) {
             Log.v("FRAGMENT", "FRAGMENT NEEDS TO BE CREATED");
             mFragment = (TabFragment) Fragment.instantiate(mActivity, mClass.getName(), (Bundle)tab.getTag());
             ft.add(android.R.id.content, mFragment, mTag);
        } else {
            Log.v("FRAGMENT", "FRAGMENT ALREADY CREATED");
            ft.show(mFragment);
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            ft.hide(mFragment);
        }
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }
}

TabFragment包含ViewPager每个选项卡的类。My issue is that when selecting a Tab other than the first one the content inside the Fragment does not show. 从片段初始化时的日志中,我可以看出正在创建视图,只是没有显示,它只是一个显示背景的空白区域。

4

3 回答 3

0

您没有分离片段,而只是隐藏它。所以你应该分离它,以便其他片段应该附加到你的onTabUnSelected.

FragmentManager将在配置更改时自动恢复当前显示的任何片段(和历史记录)。在创建和附加新实例之前调用findFragmentByTag以查看目标片段的实例是否已经存在。

例子:

public void onTabSelected(Tab tab, FragmentTransaction ft) {
    SherlockFragment preInitializedFragment = (SherlockFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag);

    // Check if the fragment is already initialized
    if (mFragment == null && preInitializedFragment == null) {
        // If not, instantiate and add it to the activity
        mFragment = (SherlockFragment) SherlockFragment.instantiate(mActivity, mClass.getName());
        ft.add(android.R.id.content, mFragment, mTag);
    } else if (mFragment != null) {
        // If it exists, simply attach it in order to show it
        ft.attach(mFragment);
    } else if (preInitializedFragment != null) {
        ft.attach(preInitializedFragment);
        mFragment = preInitializedFragment;
    }
}

onTabUnSelected应该是这样的

public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(mFragment);
        }
    }
于 2013-09-17T08:01:05.770 回答
0

而不是使用show()and hide(),使用attach()and detach()。使用显示/隐藏不会从屏幕上删除视图层次结构,只是将其隐藏,因此可能存在与此相关的问题。

于 2013-09-17T04:13:54.253 回答
0

标签栏上的内容是空的,然后写下面的代码 final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(actionBar.NAVIGATION_MODE_STANDARD);

于 2016-01-02T17:23:53.260 回答