我使用带有 ActionBarSherlock 的 ViewPager 实现创建了一个带有选项卡式视图的 ActionBar。这一切都很好,我的屏幕上有一个带有两个选项卡(配置文件和信息)的顶部栏,用 Fragments 实现。然而,这只是我的应用程序的一个部分/活动(主要活动),现在我想在屏幕底部添加另一个菜单/操作栏,在我的应用程序的不同部分之间导航。所以现在我有了主要活动,我想在底部操作栏中得到以下内容:
- 主要活动
- 设置活动
- 帮助活动
如何集成将在整个应用程序中显示的底部栏?我当前的 Activity 实现如下:
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>
任何人都知道如何解决这个问题?可能会很辛苦,但是非常感谢!