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

查看:257
本文介绍了停止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 errors,我正在使用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.

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

Basically I need to know 3 things:

  1. 如何停止标签页布局加载当前页面以外的任何页面
  2. 进入新页面后如何clear/recycle/delete旧标签页清除内存
  3. 返回标签后如何始终刷新标签
  1. How can I stop the tab layout loading any pages other than the current one
  2. How to clear/recycle/delete an old tab after you get to a new page to clear up memory
  3. How to always refresh the tab when you go back to it

推荐答案

默认情况下,它是viewpager.setOffscreenPageLimit(1),这意味着View分页器默认情况下将在当前选项卡的右侧选项卡上至少加载1,在左侧选项卡上加载至少一个.这样做是因为,当两个选项卡的某些区域可见时,在滑动Viewpager时会有一点.对于那些平稳的过渡,需要预加载.您无法将其设置为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
    }
}

仅当该特定选项卡对用户可见时才会调用此选项,因此只有这样,您才能调用所有loadfing函数.希望对您有所帮助.

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天全站免登陆