ActionBarSherlock将第二个Action Bar添加到SherlockFragmentActivity

时间:2012-07-27 18:48:14

标签: android android-fragments android-viewpager actionbarsherlock

我使用带有ActionBarSherlock的ViewPager实现创建了一个带有标签视图的ActionBar。这一切都很好,我的屏幕上有一个顶部栏,有两个标签(配置文件和信息),用Fragments实现。但是,这只是我的应用程序的一个部分/活动(主要活动),现在我想在屏幕底部添加另一个菜单/操作栏,在我的应用程序的不同部分之间导航。所以现在我有Main活动,我想在底部的操作栏中结束以下内容:

  • 主要活动
  • 设置活动
  • 帮助活动

如何整合将在整个应用程序中显示的底栏?我当前的活动实现如下:

public class ContactsActivity extends SherlockFragmentActivity implements  OnContactsFetchedListener { 

private static final String TAG = "MainActivity";

private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;
private TabHost mTabHost;

private String user;
private String token;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    setTheme(SampleList.THEME);
    super.onCreate(savedInstanceState);
    // Hide the title bar (just shows "Minggler")
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    //setContentView(R.layout.main);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String message = extras.getString("message");
        Utils.showToast(getApplicationContext(), message);
        String user = extras.getString("user");
        String token = extras.getString("token");
        if (token != null) {
            saveUniqueToken();
        }
    }

    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.pager);
    mViewPager.setOffscreenPageLimit(3);
    setContentView(mViewPager);
    mTabsAdapter = new TabsAdapter(this, mViewPager);

    ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayUseLogoEnabled(false);
    bar.setDisplayShowTitleEnabled(false);
    bar.setDisplayShowHomeEnabled(false);

    mTabsAdapter = new TabsAdapter(this, mViewPager);
    mTabsAdapter.addTab(bar.newTab().setText("Profile"),
            ContactsFragment.class, null);
    mTabsAdapter.addTab(bar.newTab().setText("Info"),
            InformationFragment.class, null);

    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    getContactInfo(intent);
    getCallHistory();
}

public static class TabsAdapter extends FragmentPagerAdapter
        implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo {
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args) {
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mActionBar = activity.getSupportActionBar();

        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

    public int getCount() {
        return mTabs.size();
    }

    public SherlockFragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return (SherlockFragment)Fragment.instantiate(mContext, info.clss.getName(), info.args);
    }

    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    public void onPageSelected(int position) {
        mActionBar.setSelectedNavigationItem(position);
    }

    public void onPageScrollStateChanged(int state) {

    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        mViewPager.setCurrentItem(tab.getPosition());
        Object tag = tab.getTag();
        for (int i=0; i<mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);
            }
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {}

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

} 

我的布局定义如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="0dip"
        android:layout_height="0dip"
        android:layout_weight="5" />

</LinearLayout>

任何人都知道如何处理这个问题?这可能需要付出很多努力,但非常感谢!

0 个答案:

没有答案