如何将PagerTabStrip放在单页中

时间:2016-12-27 06:10:51

标签: android android-viewpager fragmentpageradapter

我正在尝试设计此页面like this.

但我有这样的设计(两个片段都在那里,但看不见的页面的pagertabstrip只在我移动到此页面时看到)like this

问题面对

如何设置PagerTabStrip的宽度,以便在同一页面中显示登录和注册(在第一张图像中突出显示)。

XML:

public class Authentication extends AppCompatActivity {

    FragmentPagerAdapter adapterViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.authentication);


        SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        Boolean login_already=app_preferences.getBoolean("login",false);

        if (login_already)
        {
            startActivity(new Intent(Authentication.this,MainActivity.class));
            finish();
        }


        ViewPager vpPager = (ViewPager) findViewById(R.id.vpPager);
        adapterViewPager = new MyPagerAdapter(getSupportFragmentManager());
        vpPager.setAdapter(adapterViewPager);


        // Attach the page change listener inside the activity
        vpPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            // This method will be invoked when a new page becomes selected.
            @Override
            public void onPageSelected(int position) {
                Toast.makeText(Authentication.this,
                        "Selected page position: " + position, Toast.LENGTH_SHORT).show();
            }

            // This method will be invoked when the current page is scrolled
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // Code goes here
            }

            // Called when the scroll state changes:
            // SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
            @Override
            public void onPageScrollStateChanged(int state) {
                // Code goes here
            }
        });

    }


    public static class MyPagerAdapter extends FragmentPagerAdapter {
        private static int NUM_ITEMS = 2;

        public MyPagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        // Returns total number of pages
        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        // Returns the fragment to display for that page
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0: // Fragment # 0 - This will show FirstFragment
                    return Login.newInstance(0, "Login");
                case 1: // Fragment # 0 - This will show FirstFragment different title
                    return Registration.newInstance(1, "Register");

                default:
                    return null;
            }
        }

        // Returns the page title for the top indicator
        @Override
        public CharSequence getPageTitle(int position) {

            if (position==0)
                return  "Login";
            else
                return "Register";
            //return "Page " + position;
        }

    }
}

Java文件

{{1}}

2 个答案:

答案 0 :(得分:0)

如果您只想显示所选标签而其他标签会隐藏,请将您的PagerTabStrip设置为如下所示。

<android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        app:tabMode="scrollable"
        app:tabTextColor="#000000"
        app:tabSelectedTextColor="#ff0000"
        app:tabIndicatorColor="#ff0000"/>


app:tabTextColor=""是您正常的标签文字颜色。
app:tabSelectedTextColor=""是所选标签的颜色。
app:tabIndicatorColor=""是您选择的标签指示颜色。


因此请按照您的要求使用。有关如何使用制表符布局的详细信息,请检查此tutorial

答案 1 :(得分:0)

布局xml对我有用......

编辑:哦,我正在使用另一个lib: https://github.com/jpardogo/PagerSlidingTabStrip

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.astuetz.PagerSlidingTabStrip
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="48dip"
            android:textColor="#FFFFFFFF"
            app:pstsDividerColor="#FFFFFFFF"
            app:pstsDividerPadding="10dp"
            app:pstsDividerWidth="1dp"
            app:pstsIndicatorColor="#FF33B5E6"
            app:pstsIndicatorHeight="5dp"
            app:pstsTabPaddingLeftRight="20dip"
            app:pstsUnderlineColor="#FF33B5E6">
        </com.astuetz.PagerSlidingTabStrip>

        <FrameLayout
            android:id="@+id/movie_framelayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v4.view.ViewPager
                android:id="@+id/movie_pager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </android.support.v4.view.ViewPager>
        </FrameLayout>
    </LinearLayout>
</FrameLayout>