我想使用 ActionBar 选项卡和 ViewPager 来实现选项卡之间的滑动。我已经成功地使用了这里的示例:
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
但是,我还想在 xml 中的 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>
如果我使用 ViewPager 和 Frame 布局将 setContentView() 设置为我的布局,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 中看到选项卡,但看不到其他任何东西。不过,我的框架布局工作得很好。我怎样才能让 ViewPager 使用它?
最佳答案
你为什么这样做 mViewPager = new ViewPager(这个); mViewPager.setId(R.id.FrameLayout1); ?
ViewPager 已经在您的布局中。
应该做类似的事情 mViewPager = (ViewPager)findViewById(R.id.FrameLayout1);
他们通过示例代码在运行时创建控件,而您将它们放在 xml 中。
关于android - 具有 ViewPager 和框架布局的 ActionBar 选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14025471/