Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewpager's indicator is not moving

Tags:

android

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

With following lines:

        @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>

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.

You can see it here:

enter image description here

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.

like image 256
Okan Avatar asked Jan 27 '15 22:01

Okan


1 Answers

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

Do that tabs.setOnPageChangeListener(new OnPageChangeListener() ... )

like image 56
sockeqwe Avatar answered Oct 14 '22 03:10

sockeqwe