ViewPager skips views on return, leaving blank

I am creating an application with 4 main views. I want them all to stay on the LIST menu at the top (this is normal now).

Using fragments, I have the opportunity to change the view without actually switching to another activity, but since one of my views has a ViewPager, it does NOT work at all (when I first load the view, the content is fine, but at the second time, the ViewPager views will disappear and appear only if I go 2 index positions and go back) (see image shown for first boot time and seccond)

enter image description here

Please note that ViewPager does NOT display menu items, it is actually part of a fragment of the current menu item (in this case, "Simula'c ~ ao")

My question is: is this a known issue, is there an easy way to fix it, or should I do it differently? I already searched for this problem on the Internet, but nothing worked for me (invalidation, setting to getItemPosition to return POSITION_NONE ...)

I looked at the ViewPager class, and everything seems to be OK, I changed it, so every time I render the view, it forces me to display everything, but none of these methods worked.

I tried to set visibility to GONE and VISIBLE

I tried to disconnect and attach the adapter

Tried to cache views and return cached files.

One thing you can use to work: Change the orientation of your phone. It worked.

I tried a lot of things that I even remember ... I'm a little furious about this, because the last 24 hours have not helped me solve this "mystery" at all. I hope some of you can =]

My code: https://www.dropbox.com/s/u4mlxr3k6ta9yrm/MainActivity.java https://www.dropbox.com/s/2dqfnzjs2wl89hj/SimulationFragment.java

Views: https://www.dropbox.com/s/d6ruc1zjovqu6ob/activity_main.xml https://www.dropbox.com/s/lp1iea13klr77iq/activity_simulation.xml https://www.dropbox.com/s/1mkl3jqmo7g4wh8/ item_simulation.xml

+4
source share
3 answers

Well, after long hours trying to figure out what the hell it was, @ anup-cowkur directed me to a path that would solve my problem.

At first it seemed to me that the problem was in PageView , I downloaded the support library, edited, changed a lot of things, but there was no problem there.

Actually the problem is the FragmentPagerAdapter . It uses a kind of "caching" for each ViewPager fragment. Because of this, he was not “notified” when I returned to the view, as a result of which the last fragments shown not “relevant” exist in the parents' view (in this case, the main fragment that contains my other fragments from the ViewPager).

Since they are registered in the FragmentManager, I made a hack in the FragmentPagerAdapter by implementing this method:

FragmentPagerAdapter.java

 public void clear(ViewGroup container){ if (mCurTransaction == null) { mCurTransaction = mFragmentManager.beginTransaction(); } for(int i = 0; i < getCount(); i++){ final long itemId = getItemId(i); // Do we already have this fragment? String name = "android:switcher:" + container.getId() + ":" + itemId; Fragment fragment = mFragmentManager.findFragmentByTag(name); if(fragment != null){ mCurTransaction.detach(fragment); } } mCurTransaction.commitAllowingStateLoss(); mCurTransaction = null; } 

And then name it on my "onCreate":

myPagerAdapter.clear (mViewPager);

done. All FORCED fragments are removed from the FragmentManager, causing the cache to disappear.

This is not the best solution, but the only thing that worked.

EDIT Here you can find the hole file: http://pastebin.com/07adWVrr

+4
source

You are using the FragmentStatePagerAdapter . This adapter uses fragments inside the ViewPager. You have this ViewPager inside another parent fragment. So, you have fragments inside the fragment. Nested fragments are not supported. That is why you have problems. See: Why can't I use ViewPager inside a fragment? It is actually

It is best to use a regular PagerAdapter instead of a FragmentStatePagerAdapter. This uses regular views instead of fragments and should work fine.

0
source

If the problem occurs when you need to make several wires to get the page on the screen, this code solves it:

 mViewPager = (ViewPager)findViewById(R.id.pager); mViewPager.setOffscreenPageLimit(2); 
0
source

Source: https://habr.com/ru/post/1481299/


All Articles