简体   繁体   English

如何将 Fragment/ViewPager 标签转换为全屏

[英]How to convert Fragment/ViewPager Tabs to Full Screen

Basically what I am asking is identical to the functionality in the Twitter app, Plume, on the opening screen.基本上,我要问的与 Twitter 应用程序 Plume 中的功能在打开屏幕上相同。 On a small screen (phone), there is three tabs you can swipe back and forth.在小屏幕(手机)上,您可以来回滑动三个选项卡。 I have this exact setup.我有这个确切的设置。 On a tablet it looks bad because there is too much white space.在平板电脑上它看起来很糟糕,因为有太多的空白。 In Plume, they simply loaded all three tabs on the screen -- no swiping, they all show up and take about 1/3rd of the screen each.在 Plume 中,他们只是在屏幕上加载了所有三个选项卡——没有滑动,它们都出现了,并且每个都占据了屏幕的 1/3 左右。 Better use of space.更好地利用空间。 How do you do this?你怎么做到这一点?

Here is my code:这是我的代码:

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;
    }
}

Here is my XML Fragment layout:这是我的 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>

With your most recent answer and a working xml layout plus this code below, I think it seems simple enough!有了你最近的答案和一个有效的 xml 布局以及下面的这段代码,我认为它看起来很简单! :) :)

Edit: So the idea is you have two XML layouts, one for small and medium screens (the xml in your first post) and another for tablets, large and xlarge (the xml in your working answer).编辑:所以这个想法是你有两个 XML 布局,一个用于中小型屏幕(第一篇文章中的 xml),另一个用于平板电脑,大和超大(工作答案中的 xml)。

You could in theory switch between the two like this:理论上你可以像这样在两者之间切换:

public class YourClassName extends FragmentActivity {

    static boolean isTablet = false;
    static ActionBar actionBar = null;

public void onCreate(Bundle savedInstanceState) {
    actionBar = getActionBar();

    if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE) {     
        setContentView(R.layout.single_pane_fragment_layout);
        isTablet = true;
    } else {
        setContentView(R.layout.viewpager_fragment_layout);
    }
...

Then you could try calling this code below in onTabSelected in your fragment activity, but you may want to call it inside the fragment under onCreateView.然后,您可以尝试在片段活动中的 onTabSelected 中调用以下代码,但您可能希望在 onCreateView 下的片段中调用它。 Note I made the variables static.注意我将变量设为静态。

Example:例子:

if (YourClassName.actionBar != null && YourClassName.actionBar.isShowing() && YourClassName.isTablet) {
    YourClassName.actionBar.removeAllTabs();
}

Also here is more info about ActionBar.hide() and .removeAllTabs(): http://developer.android.com/reference/android/app/ActionBar.html#hide%28%29这里还有关于 ActionBar.hide() 和 .removeAllTabs() 的更多信息: http : //developer.android.com/reference/android/app/ActionBar.html#hide%28%29

I simply used this code from Google's site as an example:我只是以 Google 网站上的这段代码为例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/frags">
    <!-- "Fragment A" -->
  <fragment class="com.example.android.TitlesFragment"
            android:id="@+id/list_frag"
            android:layout_width="@dimen/titles_size"
            android:layout_height="match_parent"/>
    <!-- "Fragment B" -->
  <fragment class="com.example.android.DetailsFragment"
            android:id="@+id/details_frag"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
</LinearLayout>

Then I had to add the ViewPager above both Fragments.然后我不得不在两个 Fragment 上方添加ViewPager

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM