How to handle network calls inside a Fragment

前端 未结 7 1038
攒了一身酷
攒了一身酷 2021-01-31 23:21

I have the following situation:

I have an Activity that hosts a ViewPager, and I have 4 Fragments;

the ViewPager

7条回答
  •  攒了一身酷
    2021-01-31 23:38

    Basically what you want to do, is, find which fragment is currently being viewed when you swipe. And then, do your network calls.

    You can take advantage of the ViewPager listeners to get notified when the user swipes to a new page. Docs : http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html#onPageSelected(int)

    This will give you the position of the View. But i'm assuming that what you want is the actual fragment, which is a bit more tricky.

    But, this has been answered already in here : Is it possible to access the current Fragment being viewed by a ViewPager?

    Hope it helps

提交回复
热议问题