1. 简介
当我们使用ViewPager+Fragment结构时,由于谷歌在设计ViewPager时会提前加载下一个界面。虽然用户在滑动到下一页时不会感觉卡顿。但这也存在一个问题:当进入Activity时,当前界面和下一个界面均由网络请求时由于预加载了下一页导致同时有两个网络请求,在大多数情况下不显示的界面是不用联网把数据请求下来的。下面通过Fragment提供的setUserVisibleHint方法来解决该问题。
2. 具体分析
2.1 setUserVisibleHint
在分析之前先讲下setUserVisibleHint方法,官网对该方法的解释为:setUserVisibleHint
Set a hint to the system about whether this fragment's UI is currently visible to the user. This hint defaults to true and is persistent across fragment instance state save and restore.
An app may set this to false to indicate that the fragment's UI is scrolled out of visibility or is otherwise not directly visible to the user. This may be used by the system to prioritize operations such as fragment lifecycle updates or loader ordering behavior.
Note: This method may be called outside of the fragment lifecycle and thus has no ordering guarantees with regard to fra