0

I use viewpager with several fragments inside, and most of the fragments consist of a listview. I am going to examine one of them. I tried to work out endless scrolling (since I load data from an external database ) with the method suggested some people:

    if(position==getCount()-1){
        LoadMore(); 
    } 

So everytime the user scrolls down to the momentarily last item, it adds some more item to the listview. Lets say I swipe away and that fragment gets destroyed. Then after a while I go back, what happens now? The fragment has destroyed so the OnCreateView() method runs again having only the original number of items (10) loaded in the listview now. But the position of the listview is somehow at the very end (at 10), and now anything I do (scroll up and down again) the LoadMore() function never runs. The listview.getCount() is 10 again, so there should be no problem. Why is this happening? And why does the above code never runs after swiping back?

Thanks!

Jani Bela
  • 1,660
  • 4
  • 27
  • 50
  • are you using a fragmentpageradapter? – user936414 Jul 12 '13 at 11:48
  • i use FragmentStatePagerAdapter – Jani Bela Jul 12 '13 at 11:54
  • http://stackoverflow.com/a/9646622/936414 check this link – user936414 Jul 12 '13 at 11:57
  • saving the state is nothing but saving the number of items in listview in your case. By default, android keeps only one page to left and one page to right in memory. Other pages will be destroyed and when you swipe back to the destroyed page, onCreateView will be called and the fragment will be recreated. HEnce you have to save the state – user936414 Jul 12 '13 at 12:11
  • Okay I read about fragmentpageradapters, so I really have to save the state, and your link is a great help for me to understand the behaviour of saving states. I use fragments in another way so I am going to ask it in another question. – Jani Bela Jul 12 '13 at 13:06

1 Answers1

1

Because list are 0 indexed shouldn't you be checking:

if(position==getCount()-1){ //check for the last item ?
    LoadMore(); 
} 
Gomino
  • 12,127
  • 4
  • 40
  • 49
  • I actually use -3, since I want to load those new bunch of data when the user is close to the end and not when is he at the very end. I am going to rewrite it in my question. – Jani Bela Jul 12 '13 at 11:56
  • I suppose you're doing this check in your getView method of your adapter. Well can you log if it is actually getting call and print the value of position and getCount()? – Gomino Jul 12 '13 at 12:37
  • Yes i am doing it in the getView. I put a log after the LoadMore() and it is getting called, so it relaizes that it should add another 10 items to the listview, since the log tells me the getcount is 20. – Jani Bela Jul 12 '13 at 12:53
  • Then are you sure to call notifyDataSetChanged() after you finish loading more datas ? – Gomino Jul 12 '13 at 13:08
  • Wow I missplaced it.. Thank you, but I just realized that saving the state would be a better solution, I am going to ask it in another Q. – Jani Bela Jul 12 '13 at 13:12