B
B
Benderlidze2014-04-10 11:43:31
Android
Benderlidze, 2014-04-10 11:43:31

How to update FragmentStatePagerAdapter?

The situation is this - there are fragments, in one of the fragments there is a ViewPager for 3 pages.
I select a city, I go to the fragment with the ViewPager - and the data is displayed in it as it should, then I go back to another fragment, I select another city or object, it doesn’t matter, and I go back to the fragment with the ViewPager and the pages are empty, they are updated only after turning and then only the 1st and 3rd pages, the second is always empty.
It seems that the data is updated and transmitted correctly, according to the log they are, but they are not immediately displayed, but only after flipping. Here is the adapter code.

private class MyPagerAdapter extends FragmentStatePagerAdapter{

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

        @Override
        public int getItemPosition(Object object) {
            Log.d("getItemPosition","123");
            return POSITION_NONE;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Resources res = getResources();
            String[] title = res.getStringArray(R.array.title_spa_viewpager_array);
            return title[position];
        }

        @Override
        public android.support.v4.app.Fragment getItem(int pos) {
            switch(pos) {
                case 0: return FirstFragment.newInstance(mSpa.getSpaName()+SpaInfoParserObject.MySpaInfo.getSpaInfoText());
                case 1: return SecondFragment.newInstance("",SpaInfoParserObject.MySpaInfo);
                case 2: return PhotoFragment.newInstance("", SpaInfoParserObject.MySpaInfo.getSpaInfoPic(), mSpa);
                default: return FirstFragment.newInstance(mSpa.getSpaPhone());
            }
        }

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Yakushev, 2014-04-10
@VYakushev

Note that in getItem() , the fragments are always re-created using the newInstance() method . That is, in fact, you have only one fragment "alive", so the data is displayed only after paging.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question