Android嵌套标签导航4.2

时间:2013-01-15 18:21:02

标签: android tabs

我在主页导航中使用ActionBar,其中包含应用程序中不同区域的四个选项卡。

其中一个区域是一个地图部分,它应包含两个子选项卡,以显示从SQLite数据库填充的项目列表。

基本上,我想从我的页面片段实现嵌套导航。我到目前为止的代码是:

   //MainActivity.java

            ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(true);

    actionBar.setCustomView(R.layout.rowlayout);

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    LayoutInflater inflater = (LayoutInflater)         this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout layoutView = (RelativeLayout)inflater.inflate(R.layout.action_bar_tab,   null);
    TextView tabText = (TextView) layoutView.findViewById(R.id.tabText);
    ImageView tabImage = (ImageView) layoutView.findViewById(R.id.tabImage);


    String dayOneName = getResources().getString(R.string.day_one);
    Tab tab = actionBar.newTab();
    tab.setIcon(R.drawable.cal);
    TabListener<AgendaMain> dayOne = new TabListener<AgendaMain>(this,
            dayOneName, AgendaMain.class);
    tab.setTabListener(dayOne); 
    // set custom view
    tabText.setText(dayOneName);
    tabImage.setImageResource(R.drawable.cal);
    tab.setCustomView(layoutView);
    actionBar.addTab(tab);

    // Plus three more navigation tabs


  private class TabListener<T extends Fragment> implements
    ActionBar.TabListener {

    private Fragment 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;
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // Check if the fragment is already initialized
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach(mFragment);
        }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(mFragment);
        }
    }
    @Override
    public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
        // User selected the already selected tab. Usually do nothing.
    }
}

上面的标签链接到片段,我想在其中添加两个新标签,两个标签显示一个ListFragment。

我尝试从here实现FragmentTabHost,但遇到android.support.v4.app与MainActivity.java中的TabListener之间的问题。到目前为止,所有代码都在运行,只是尝试向片段添加标签!

如果有人可以建议/展示实现这个的最佳方法,那将非常感激(:我到目前为止只设法显示静态内容,这是不好的!(如果需要,我会发布更多我的代码,我怀疑这会有所帮助,因为我没有运气!)

1 个答案:

答案 0 :(得分:0)