Viewpager的指示器没有移动

Viewpager的指示器没有移动

本文介绍了Viewpager的指示器没有移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用寻呼机滑动标签条: https://github.com/astuetz/PagerSlidingTabStrip/

I am using pager sliding tab strip : https://github.com/astuetz/PagerSlidingTabStrip/

带有以下几行:

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            pager =  (ViewPager) findViewById(R.id.pager);
            pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
            tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
            tabs.setViewPager(pager);
        }

MyPagerAdapter:

public class MyPagerAdapter extends FragmentPagerAdapter {

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public CharSequence getPageTitle(int position) {
        return (position == 0)? "Users" : "Chats" ;
    }
    @Override
    public int getCount() {
        return 2;
    }
    @Override
    public Fragment getItem(int position) {
        return (position == 0)?  new RandomsFragment() : new ChatsFragment() ;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
}

Xml:

<com.astuetz.PagerSlidingTabStrip
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="48dip"
    app:pstsIndicatorColor="#0B0B16"
    app:pstsShouldExpand="true" />

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

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tabs">
</android.support.v4.view.ViewPager>

上面的代码一切都可以正常工作.但是我需要听制表符的更改,为实现此目的,我将 onCreate 方法更改为:

Everything is working fine with at above code.But I need to listen tab changes,for achive this I changed my onCreate method to this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        pager =  (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        tabs.setViewPager(pager);
        pager.setOnPageChangeListener(new OnPageChangeListener() {
              public void onPageScrollStateChanged(int state) {}
              public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
              public void onPageSelected(int position) {
                  Log.w("Tab Selected","Position:"+position);
          }
    });
    }

使用此代码,我的代码可以工作,但选项卡指示符没有移动.

With this code my code is working but tab indicator is not moving.

您可以在这里看到它:

我在聊天"标签中,但指示器仍在用户"标签上.如何解决此问题?如果我不使用 setOnPageChangeListener ,它就会移动.

I am in chats tab but indicator is still on the users tab.How can i fix this ? If I don't use setOnPageChangeListener it is moving.

推荐答案

问题在于 pager.setOnPageChangeListener(new OnPageChangeListener()),因为选项卡本身设置为OnPageChangeListener.

the problem is pager.setOnPageChangeListener(new OnPageChangeListener()) because the tabs itself are set as OnPageChangeListener.

执行 tabs.setOnPageChangeListener(new OnPageChangeListener()...)

这篇关于Viewpager的指示器没有移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!