0

首先,我得到了我的支持库,我绝对不想使用 sherlock 或类似的支持库。

这是我的活动代码:

    public class MyClass ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_class);

    // Get the message from the intent
    Intent intent = getIntent();
}
static ActionBar actionBar;
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    //       Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater upInflater = getMenuInflater();
    upInflater.inflate(R.menu.action_bar, menu);
    // setup action bar for tabs
    actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    Tab tab = actionBar.newTab()
            .setIcon(R.drawable.ic_action_home)
            .setTabListener(new TabListener<Fragment1>(
                    this, home, Fragment1.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab()
            .setIcon(R.drawable.ic_action_cart)
            .setTabListener(new TabListener<Fragment2>(
                    this, cart, Fragment2.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab()
            .setIcon(R.drawable.ic_action_users)
            .setTabListener(new TabListener<Fragment3>(
                    this, users, Fragment3.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab()
            .setIcon(R.drawable.ic_action_products)
            .setTabListener(new TabListener<Fragment4>(
                    this, products, Fragment4.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab()
            .setIcon(R.drawable.ic_action_settings)
            .setTabListener(new TabListener<Fragment5>(
                    this, settings, Fragment5.class));
    actionBar.addTab(tab);

    return super.onCreateOptionsMenu(menu);
}

如您所见,我使用片段在选项卡之间移动,这也是我的 TabListener:

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

    /** Constructor used each time a new tab is created.
      * @param activity  The host Activity, used to instantiate the fragment
      * @param tag  The identifier tag for the fragment
      * @param clz  The fragment's Class, used to instantiate the fragment
      */
    public TabListener(Activity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    /* The following are each of the ActionBar.TabListener callbacks */

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // Check if the fragment is already initialized
        actionBar.setTitle(mTag);// <===
        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);
        }
    }

    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.
    }
}

这完全可以单独使用。我也为每个片段准备好了布局和类。但我似乎无法ViewPager为这种代码风格找到合适的来源。我走错方向了吗?我应该怎么办?

提前致谢。

4

1 回答 1

0

我通过eclipse帮助解决了这个问题。我创建了一个具有滑动功能的项目,它适用于高于 11 的 api 级别。我编辑了整个代码,并在支持库 appcompat 7 和 4 的帮助下使其工作。它非常简单并且运行良好。

于 2013-10-12T07:58:26.563 回答