androidandroid-viewpagerfragmentpageradapterpagerslidingtabstrip

Switching tabs not working on click of tabs with PagerSlidingTabStrips


I've integrated PagerSlidingTabStrips in my application which runs as expected on Swipe of tabs.

But selecting tabs on PagerSlidingTabStrips doesn't switch to that fragment which works perfectly on swiping between tabs.

FragmentManager fm = getSupportFragmentManager();
mPager = (ViewPager) findViewById(R.id.pager);
ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm);
mPager.setAdapter(viewpageradapter);
pagerSlidingTabStrp = (PagerSlidingTabStrip) findViewById(R.id.pager_sliding_tab_strip);
pagerSlidingTabStrp.setShouldExpand(true);
pagerSlidingTabStrp.setViewPager(mPager);
pagerSlidingTabStrp.setOnPageChangeListener(ViewPagerListener);     


// Capture ViewPager page swipes
    ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            switch(position)
            {
            case 0:
                setMotherActionBarTitle(getString(R.string.str_all_contacts_fragment));
                break;
            case 1:
                setMotherActionBarTitle(getString(R.string.str_group_contacts_fragment));
                break;
            case 2:
                setMotherActionBarTitle(getString(R.string.str_call_logs_fragment));
                break;                  
            }
        }
    };

ViewPagerAdapter

public class ViewPagerAdapter extends FragmentPagerAdapter implements IconTabProvider {
final int PAGE_COUNT = 3;
private final int[] ICONS = { R.drawable.tab_icon_zname_contact_selector, R.drawable.tab_icon_zname_friends_selector, 
        R.drawable.tab_icon_zname_call_log_selector };

public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}


@Override
public Fragment getItem(int item) {
    switch (item) {

    case 0:
        ContactsFragment homeFragment = new ContactsFragment();
        return homeFragment;
    case 1:
        GroupsFragment groupsFragment = new GroupsFragment();
        return groupsFragment;
    case 2:
        CallLogsFragment callLogsFragment = new CallLogsFragment();
        return callLogsFragment;
    }
    return null;
}

@Override
public int getCount() {
    return PAGE_COUNT;
}

@Override
public int getPageIconResId(int position) {
    return ICONS[position];
 }

} 

How to make selecting tabs works with PagerSlidingTabStrips? Morever PagerSlidingTabStrips tabs are not even clickabe.

Did I missed out to implement something? Or to add pagerSlidingTabStrips.setClickable(true)? or something?

Again the selector of PagerSlidingTabStrips tabs not selected proper drawable with selector. What could be possible the reason for that?

Here's a picture of it. PagerSlidingTabStrips


Solution

  • I've found out why clicking tab was not working with PagerSlidingTabStrips after I've looked out for other options with sliding tabs functionality like SlidingTabsLayout which also was not able to click tabs.

    Found out problem was with my xml layout which covers whole as ViewPager which doesn't make tabs clickable of eitherPagerSlidingTabStrips or SlidingTabsLayout works.

            <com.netdoers.zname.utils.PagerSlidingTabStrip
                android:id="@+id/pager_sliding_tab_strip"
                android:layout_width="fill_parent"
                android:layout_height="45dip"
                android:background="@android:color/white"
                app:pstsDividerColor="#FFFFA500"
                app:pstsIndicatorColor="#FFFFA500"
                app:pstsTabPaddingLeftRight="14dip"
                app:pstsUnderlineColor="#FFFFA500" />
    
    
            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="45dp" />
    

    Leaving same height in Viewpager as of PagerSlidingTabStips from top where PagerSlidingTabStrips rendered solved my issue.