我想使用ActionBar选项卡和ViewPager来实现在选项卡之间滑动。我已经成功地使用了这里的示例:http://developer.android.com/reference/android/support/v4/view/ViewPager.html。但是,我还想在xml中添加一个ViewPager之外的Frame Layout:
如果我将setContentView()设置为同时包含ViewPager和Frame Layout的布局,则ViewPager停止工作:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
<FrameLayout
android:id="@+id/frame_layout"
android:name="com.banagas.calculator.ButtonFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
如果我将setContentView()设置为同时包含ViewPager和Frame Layout的布局,则ViewPager停止工作:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("MainActivity", "onCreate");
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.FrameLayout1);
ActionBar bar = getSupportActionBar();
mViewPager.setOffscreenPageLimit(4);
bar.setDisplayHomeAsUpEnabled(false);
bar.setDisplayShowTitleEnabled(false);
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText("Display"), DisplayFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Y="), YFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Graph"), GraphFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Table"), TableFragment.class, null);
if (findViewById(R.id.frame_layout) != null)
{
if (savedInstanceState != null)
{
return;
}
getSupportFragmentManager().beginTransaction().add(R.id.frame_layout, new ButtonFragment()).commit();
Log.i("MainActivity", "add.ButtonFragment");
}
}
我能看到ViewPager中的选项卡,但除此之外什么也看不到。我的Frame Layout却完美地运行着。我该如何让ViewPager与它一起工作?