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:
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.
the problem is pager.setOnPageChangeListener(new OnPageChangeListener())
because the tabs itself are set as OnPageChangeListener.
Do that tabs.setOnPageChangeListener(new OnPageChangeListener() ... )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With