Question about fragments and viewPagers

srensren DKMember

Hey

I get a viewPager build up by an FragmentAdapter that contains fragments(each fragment = 1 page on my viewPager). now i know that all my fragment gets created when my viewPager gets created.

Is there a way to make so a fragment only gets created when you swipe/fling to it via the viewPager? or is there a way to track when you swipe/fling to a certain fragment? The reason why i want to do this is, that i got each fragment loading a thread with a httprequest, and i dont want all requests to start at the same time(i only want to start them if the user swipes to the fragment = the user has asked to see the data).

Atm i found out that i can implement ViewPager.IOnPageChangeListener, and set it on the indicators for my viewpagers, but this solution would make all fragments start the same thread( and i want them to start different threads).

Thanks for your time, hope someone out there can help me.

Posts

  • TeHaTeHa ✭✭✭ DEMember ✭✭✭

    I don't think that all your fragments are created when your viewPager is created.
    But you can try to set OffscreenPageLimit to 0.

  • srensren DKMember

    OffscreenPageLimit cant be 0, needs to be minimum 1:(, and when i debug it i can see it creates my fragments even the ones not on screen, and it does this on activity load. So either I've done something to my code so its creating the fragments or its like this by default. I know that I've told my fragmentAdaptor to create all fragments(so when i initialize my adaptor all fragments gets created), is this my mistake perhaps? if thats the case how should i create the fragments?

  • FZelleFZelle ✭✭✭ DEMember ✭✭✭

    Just because your fragment is created doesn't mean that you have to start the dataprocessing then.

    Why don't you start that when you change to that page? To easy?

  • srensren DKMember

    How do i do that?

  • StevaSteva ✭✭✭ RSMember ✭✭✭

    Implement ViewPager.IOnPageChangeListener in your activity and do your http requests inside OnPageSelected method.

Sign In or Register to comment.