用ViewPager布局,但是又不想预加载的问题

​问题背景:

    ViewPager是常用来做tab分页布局的一个组件,程序员常用ViewPager+Fragment来做整体布局

问题描述:

    有时候在某个页面显示的时候,会去加载一些数据,但这些数据在某些情况下又是不需要加载的,如果程序员习惯将这个加载操作放在Fragment的CreateView中,因为ViewPager会预加载页面以便滑动,则会造成这些数据在不恰当的时候过早的被加载调用,此时,即便是将ViewPager的setOffscreenPageLimit(0)设置为0也是无效的,因为ViewPager为了避免滚动时出现空页的情况,无视了为0这种操作


解决方案:

@Override
public void setUserVisibleHint(boolean isVisibleToUser){
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        //当显示给用户时
    } else {
        //当不显示给用户时
    }
}


官方对此函数的解释:

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.


大意:这个函数是系统决定fragment是否呈现在用户面前的一个提示。当fragment滑动出可视范围是,会被设置为false,反之则为true,这个函数可以用于fragment显示给用户看是,处理一些更新或加载的行为

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值