How to know when fragment actually visible in viewpager

后端 未结 4 1672
走了就别回头了
走了就别回头了 2021-01-12 16:41

I am using 4 fragments inside a ViewPager ,as ViewPager load the previous and next fragment in advance ,and no lifecycle method is called when navigating between fragment

4条回答
  •  春和景丽
    2021-01-12 16:52

    Of course. Assuming that viewPager is your instance of the ViewPager, use: viewPager.getCurrentItem().

    Within your Fragment you can check if its instance is visible to the user like so:

    @Override
    public void setUserVisibleHint(boolean visible) {
        super.setUserVisibleHint(visible);
        if (visible) {
            Log.i("Tag", "Reload fragment");
        }
    }
    

    Always make sure that you search for answers throughly before asking your question. For instance, the first place you should check would be: https://developer.android.com/reference/android/support/v4/view/ViewPager.html

提交回复
热议问题