android - Astuetz 的 PagerSlidingTabStrip 库还是不能滑动 tabStrip

标签 android tabs pagerslidingtabstrip

我添加了 PagerSlidingTabStrip library到我的项目,按照指南进行操作,但选项卡下的 strip 不会滑动(我指的是您可以在 Google Play 应用程序中看到的内容)。 这是我的 MainActivity:

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mCollectionPagerAdapter);

    PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabs.setViewPager(mViewPager);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

        @Override

        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }

    });
    for (int i = 0; i < mCollectionPagerAdapter.getCount(); i++) {
        actionBar.addTab(actionBar.newTab()
                .setText(mCollectionPagerAdapter.getPageTitle(i))
                .setTabListener(this));

    }

编辑:

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.astuetz.PagerSlidingTabStrip
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="48dip"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity" />

</RelativeLayout>

主要 Activity :

public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener, android.app.ActionBar.TabListener {
CollectionPagerAdapter mCollectionPagerAdapter;
ViewPager mViewPager;
SharedPreferences mPrefs;
final String welcomeScreenShownPref = "welcomeScreenShown";


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setProgressBarIndeterminateVisibility(true);
    setProgressBarIndeterminateVisibility(false);
    setContentView(R.layout.activity_main);


    mCollectionPagerAdapter = new CollectionPagerAdapter(
            getSupportFragmentManager());

    final android.app.ActionBar actionBar = getActionBar();
    assert actionBar != null;
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setIcon(R.drawable.ic_action_icon3);
    actionBar.setTitle(R.string.app_name2);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mCollectionPagerAdapter);

    PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabs.setViewPager(mViewPager);


}

编辑2 tabs_background.xml:

<item android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_example" />
<item android:state_focused="true" android:drawable="@drawable/tab_selected_focused_example"/>
<item android:drawable="@color/tab_color"/>

主要 Activity :

actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setIcon(R.drawable.ic_action_icon3);
    actionBar.setTitle(R.string.app_name2);

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mCollectionPagerAdapter);

    PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabs.setViewPager(mViewPager);
    tabs.setIndicatorColor(android.R.color.white);

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.astuetz.PagerSlidingTabStrip
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="48dip"
    app:pstsIndicatorColor="@android:color/white"
    app:pstsUnderlineColor="@color/tab_color"
    app:pstsDividerColor="@color/tab_color"
    app:pstsTabBackground="@drawable/tabs_background"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_below="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity" />

</RelativeLayout>

最佳答案

如果您想使用 PagerSlidingTabStrip,则无需向 actionBar 添加标签。只需将您的代码剪切为:

mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCollectionPagerAdapter);

PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(mViewPager);

库自动将标签添加到操作栏,通过此方法获取标签文本:

@Override
public CharSequence getPageTitle(int position) {
    return TITLES[position];
}

编辑:

android:layout_below 属性添加到 ViewPager,它应该可以工作:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="48dip"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_below="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context=".MainActivity" />

</RelativeLayout>

编辑 2:

首先,在你的 drawable 文件夹中创建一个 selector,名为 tabs_background.xml 用于标签背景:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:drawable="@color/your_pressed_red_color" />
    <item android:state_focused="true" android:drawable="@color/your_focused_red_color"/>
    <item android:drawable="@color/your_red_color"/>

</selector>

然后就可以自定义tabs了,通过设置属性,可以看到here ,至于你的定制:

<com.astuetz.PagerSlidingTabStrip
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="48dip"
    app:pstsIndicatorColor="@android:color/white"
    app:pstsUnderlineColor="@color/your_red_color"
    app:pstsDividerColor="@color/your_red_color"
    app:pstsTabBackground="@drawable/tabs_background">

不要忘记将这一行添加到父 RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"

最后,要设置文本颜色,以编程方式进行:

tabs.setTextColor(android.R.color.white);

关于android - Astuetz 的 PagerSlidingTabStrip 库还是不能滑动 tabStrip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26741551/

相关文章:

android - iOS中Webview Flutter字体太小

android - 以编程方式使用选项卡更改 fragment 的选项卡索引

android twitter 应用程序,自定义 viewpager 选项卡如何

android - 图标选择器不适用于 PagerSlidingTabStrips

java - 如何设置 OnClickListener (Android)

Android 网络传输对象

PHP HTML\t 和 Tab 问题

android - 如何将滑动选项卡 View 放入抽屉导航选项之一

android - 在android中使用PagerSlidingTabStrip时如何默认加载第二个选项卡

android - android :text of EditText while the android:text is left undefined?的默认值是多少