Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why it changes the pages in my viewpager

 
sam liya
Ranch Hand
Posts: 1243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have view pager with several fragments.Each fragment contain a pdf file.When i travel through right side and when i come back through the page changes than earlier one.As example earlier first page changes to new pdf page.How it happen?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's possibly a bug in your code. Be aware that a ViewPager is free to discard fragments, so your Fragment and FragmentPagerAdapter classes need to be prepared to recreate their content at all times.
 
sam liya
Ranch Hand
Posts: 1243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am adding fragments dynamically.below is my fragment code



Below is my main class



In each fragment i am opening a pdf file which containing a single page.But when i swipe unti end and comes back it shows me different pages.i used count variable to to get the pdf file name.(pdf file names are 1.pdf,2.pdf,3.pdf,4.pdf,5.pdf)
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't picture what "it shows me different pages" might look like - post a much more detailed description of what happens. Also insert logging calls into MyAdapter.getItem so you know when a fragment for which position was requested. Include the sequence of those calls with position parameter as part of the detailed description. Also include the sequence of calls to onPageScrolled and onPageSelected, and include the printout of all their parameters.
 
sam liya
Ranch Hand
Posts: 1243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I open a different pdf file in each fragment.There are 5 pdf files.So when i swipe right it open a each pdf file continuously.But when i come back(Swipe left)the pdf file file order is change(2nd page contain different pdf file that earlier).I added log messages to position itm and the fragment constructor.

 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is very confusing - for example, something that looks like a file name is being passed to MyFragment.newInstance, but that is apparently not a file name, and not being used as such; I hope that is not what is being for the logcat output, because that would be meaningless.

I also see a method "addPaths" which looks like it should be called once (statically), but which in fact is called multiple times, so the "files" list would be rather long and contain all files multiple times - bug? And then it's used in line 60, but not in line 63 - causing further confusion: either use it or don't (in which case, delete it), but using it sometimes is bad.

Then there is the onCreateView method, which creates two different paths in lines 56 and 62. I'm not familiar with the PDF library, but that looks highly suspicious. The whole business with the "count" variable also looks wrong - is assumes that getItem is called in order from 0 to 4, which (as I mentioned earlier) is most likely not true.

What should happen instead of all this is that the getItem method passes the position to the fragment where it is stored. Then onCreateView uses that position to display whatever PDF should be displayed at that position.
 
sam liya
Ranch Hand
Posts: 1243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ulf foe helping.I found the error.I am addding the paths inside the fragment.Now i change it.Now i am adding paths inside activity.Now its working fine.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic