停止 Android TabLayout 自动加载下一页 [英] Stop Android TabLayout loading next page automatically

查看:17
本文介绍了停止 Android TabLayout 自动加载下一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经意识到,在 Android 中使用选项卡布局时,它总是会加载与之接触的选项卡,即之前的选项卡和之后的选项卡,因此在您分页时会加载它.

I've realized when using a tab layout in Android it always loads the tabs touching it, i.e. the tab before and the tab after so it is loaded when you page to it.

但是,我从服务器加载大量内容和图像,这会导致大量数据和内存使用,并且我经常遇到 OOM 错误,我正在使用 Glide 有效地显示图像.

However, I load lots of content and images from a server and this causes a lot of data and memory use and I often get OOM errors, I am displaying the images efficiently using Glide.

基本上我需要知道 3 件事:

Basically I need to know 3 things:

  1. 如何停止标签布局加载当前页面以外的任何页面
  2. 进入新页面后如何清除/回收/删除旧标签以清理内存
  3. 如何在返回标签页时始终刷新标签页

推荐答案

默认是 viewpager.setOffscreenPageLimit(1) ,意思是 View pager 默认加载至少 1 个在当前标签页的右边和一个在左边的标签页.这样做,主要是因为当你滑动查看器时,两个选项卡的某些区域都是可见的.对于那些平滑过渡,需要预加载.您不能设置 viewpager.setOffscreenPageLimit(0).唯一的出路是使用这个方法 setUserVisibleHint将此添加到您的片段中

By default it is viewpager.setOffscreenPageLimit(1) , meaning View pager will by default load atleast 1 on the right and one on the left tab of current tab. It is done so, mostly because there is a point when u slide viewpager, when certain area of both tabs is visible. For those smooth transitions preloading is required. You cannot set it viewpager.setOffscreenPageLimit(0). The only way out is to use this method setUserVisibleHint add this to your fragment

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        // load data here
    }else{
       // fragment is no longer visible
    }
}

只有当该特定选项卡对用户可见时才会调用,所以只有这样你才能调用所有加载函数.希望对您有所帮助.

This will be called only when that particular tab is visible to user, so only then u can call all loadfing function. Hope it helps.

这篇关于停止 Android TabLayout 自动加载下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆