带标签的 Android Actionbar Sherlock [英] Android Actionbar Sherlock with Tabs

查看:35
本文介绍了带标签的 Android Actionbar Sherlock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用下面的选项卡实现 ActionBar Sherlock,如上图所示.

I am trying to implement ActionBar Sherlock with Tabs below that as shown in the above wire-frame.

我应该使用 TabActivity 吗?- 因为我看到它已被弃用.这是实现相同目标的最佳方法.

Should i use TabActivity ? - since i saw that it is deprecated. Which is the best way to achieve the same.

推荐答案

我使用 SherlockFragmentActivity 作为 tabview 容器并使用 SherlockFragment 作为选项卡实现了这个功能.这是一个草图(我省略了通常的 Android 活动内容):

I implemented this functionality with a SherlockFragmentActivity as tabview container and with SherlockFragment as tabs. Here is a sketch (I omitted the usual Android activity stuff):

这是带有两个标签的 tabview 活动:

This is the tabview activity with two tabs:

public class TabViewActivity extends SherlockFragmentActivity {
  // store the active tab here
  public static String ACTIVE_TAB = "activeTab";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    ..
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    // add the first tab which is an instance of TabFragment1
    Tab tab1 = actionBar.newTab()
              .setText("TabTitle1")
              .setTabListener(new TabListener<TabFragment1>(
               this, "tab1", TabFragment1.class));
    actionBar.addTab(tab1);

    // add the second tab which is an instance of TabFragment2
    Tab tab2 = actionBar.newTab()
           .setText("TabTitle2")
           .setTabListener(new TabListener<TabFragment2>(
                this, "tab2", TabFragment2.class));
    actionBar.addTab(tab2);

    // check if there is a saved state to select active tab
    if( savedInstanceState != null ){
      getSupportActionBar().setSelectedNavigationItem(
                  savedInstanceState.getInt(ACTIVE_TAB));
    }
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    // save active tab
    outState.putInt(ACTIVE_TAB,
            getSupportActionBar().getSelectedNavigationIndex());
    super.onSaveInstanceState(outState);
  }
}

这是保存标签内容的TabFragment:

public class TabFragment extends SherlockFragment {
  // your member variables here
  @Override
  public View onCreateView(LayoutInflater inflater, 
                 ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_va_esh, container, false);
    ... // do your view initialization here
    return view;
  }

}

最后是处理标签切换的TabListener:

And finally this is the TabListener that handles tab switches:

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 initialized
    if (mFragment == null) {
      // If not, instantiate and add it to the activity
      mFragment = (TabFragment) Fragment.instantiate(
                        mActivity, mClass.getName());
      mFragment.setProviderId(mTag); // id for event provider
      ft.add(android.R.id.content, mFragment, mTag);
    } else {
      // If it exists, simply attach it in order to show it
      ft.attach(mFragment);
    }

  }

  public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    if (mFragment != null) {
      // Detach the fragment, because another one is being attached
      ft.detach(mFragment);
    }
  }

  public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // User selected the already selected tab. Usually do nothing.
  }
}

这篇关于带标签的 Android Actionbar Sherlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆