PagerSlidingTabStrip icon indicating copy to clipboard operation
PagerSlidingTabStrip copied to clipboard

pager.setCurrentItem issue

Open AlexMercier opened this issue 10 years ago • 6 comments

hello. help me pls, after initialize tabs and pager i call pager.setCurrentItem(1) but tabs show me 3rd selected tab?

        pager = (ViewPager) v.findViewById(R.id.pager);
        adapter = new MyPagerAdapter(getActivity().getSupportFragmentManager());
        pager.setAdapter(adapter);

        tabs = (PagerSlidingTabStrip) v.findViewById(R.id.tabs);
        tabs.setViewPager(pager);
        tabs.setIndicatorColor(Color.parseColor("#ff7676"));
        pager.setCurrentItem(1);

my code is clear, i dont call setCurrentItem(2) somewhere..., but its position is 2. Image and i try to set nexr one, setCurrentItem(3) but this show me then 5 tab selected instead of 4 by order. what i miss? the full code is here: http://pastebin.com/raw.php?i=zdNTHyKn tested on emulator android 5, 4.2.2 and real device 4.2.2

also one more problem, after rotating screen the onCreateView for pager fragments didnt call and i have empty content on pages. see the screenshot: after rotating the Button not visible and background is grayed (because onCreateView not called and view layout not fetched) screenshot after rotating: http://i1.imageban.ru/out/2015/04/24/ac1f12d3412548817dce3f5ad7e88bc7.png

AlexMercier avatar Apr 24 '15 17:04 AlexMercier

  1. Use getChildFragmentManager() instead of getSupportFragmentManager()
  2. Have MyPagerAdapter extend FragmentStatePagerAdapter instead of FragmentPagerAdapter

theBlbDan avatar Apr 25 '15 21:04 theBlbDan

I am facing slightly different problem with my similar application. I am attaching new fragment with button and listview for each tab. Lists in all tabs are populated from web service using AsyncTask and common filter(initally no filter). In Tab 1, when user clicks a button, a dialog fragment appears and user selects a filter. When the dialog fragment is closed, I need to update the Tab 1 listView. I tried doing with notifyDataSetChanged but it is updating the Tab 2 list instead of the current tab. Ideally it should affect both Tabs(all tabs based on selected option). As i know, pagerSlidingTabStrip downloads 2-3 tabs based on the position. I can notice changes in other tabs except the current tab i am in. If i move across tabs and come back, i can see the change as the older fragment will destroy and creates new fragment. How to get control of the current tab or refresh all tabs?

sunaysp avatar Apr 28 '15 18:04 sunaysp

Are you using LoaderManager? If so, I found that each tab's fragment needs to use a unique loaderID in getLoaderManager().initLoader(loaderID, bundle, this);

Since the loader is created in the main activity's context, you can't use loaderID = 0 for each fragment in this type of situation or it will hand off the callback to the wrong fragment.

Check out this article. http://www.androiddesignpatterns.com/2012/07/understanding-loadermanager.html

theBlbDan avatar Apr 28 '15 19:04 theBlbDan

it is a bug.

you can fix it by adding this line to the PagerSlidingTabStrip.java

currentPositionOffset = 0;//source line number 220

getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    @Override
    public void onGlobalLayout() {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            getViewTreeObserver().removeGlobalOnLayoutListener(this);
        } else {
            getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }

        currentPosition = pager.getCurrentItem();
        currentPositionOffset = 0;//source line number 220
        scrollToChild(currentPosition, 0);
    }
});

ubdc avatar Apr 29 '15 03:04 ubdc

Thank you for the response. I tried but it didn't fix the issue. I am still not able to apply the new filter for the pages(tabs) already downloaded. I am not using LoaderManager(). Is there anyway we can initiate the download again?

sunaysp avatar May 12 '15 17:05 sunaysp

whooo... It's working...great solution

abhisheklunagaria avatar Dec 03 '15 06:12 abhisheklunagaria