基本上,我要问的内容与打开屏幕上的 Twitter 应用程序 Plume 中的功能相同。在小屏幕(手机)上,您可以来回滑动三个选项卡。我有这个确切的设置。在平板电脑上它看起来很糟糕,因为有太多的空白。在 Plume 中,他们只是在屏幕上加载了所有三个选项卡——没有滑动,它们都显示出来,每个都占据屏幕的 1/3。更好地利用空间。你怎么做到这一点?
这是我的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOffscreenPageLimit(3);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
catName = getSharedPreferences("catName", Context.MODE_PRIVATE);
itemName = getSharedPreferences("itemName", Context.MODE_PRIVATE);
mViewPager.setCurrentItem(1);
}
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment f = null;
switch (position) {
case 0: {
f = new MasterFrag();
Bundle args = new Bundle();
f.setArguments(args);
break;
}
case 1: {
f = new FeaturedFrag();
Bundle args = new Bundle();
f.setArguments(args);
break;
}
case 2: {
f = new TopFrag();
Bundle args = new Bundle();
f.setArguments(args);
break;
}
default:
throw new IllegalArgumentException("not this many fragments: "
+ position);
}
return f;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.mastercattab1).toUpperCase(Locale.ENGLISH);
case 1:
return getString(R.string.mastercattab2).toUpperCase(Locale.ENGLISH);
case 2:
return getString(R.string.mastercattab3).toUpperCase(Locale.ENGLISH);
}
return null;
}
}
这是我的 XML 片段布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</TabHost>
</LinearLayout>