If you use Kotlin, you can do the following:
1. On first, you should be create Interface
and implemented him in your Fragment
interface RefreshData {
fun refresh()
}
class YourFragment : Fragment(), RefreshData {
...
override fun refresh() {
//do what you want
}
}
2. Next step is add OnPageChangeListener
to your ViewPager
viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) { }
override fun onPageSelected(position: Int) {
viewPagerAdapter.notifyDataSetChanged()
viewPager.currentItem = position
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { }
})
3. override getItemPosition
in your Adapter
override fun getItemPosition(obj: Any): Int {
if (obj is RefreshData) {
obj.refresh()
}
return super.getItemPosition(obj)
}