第二个片段(选项卡)的onCreateView在第一个片段上被调用 [英] onCreateView of second fragment (tab) is called while on the first

查看:163
本文介绍了第二个片段(选项卡)的onCreateView在第一个片段上被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的activity中有10个标签.一旦我打开活动,就会显示第一个片段,但是会调用下一个片段中存在的方法(AsyncTask).并且如果我转到下一个选项卡,说第3个选项卡,则调用第4个fragment中存在的方法,依此类推.

I have 10 tabs in my activity. As soon as I open the activity the first fragment gets displayed but the method (AsyncTask) present in the next fragment gets called. And if I go to the next tab say 3rd tab then the method present in the 4th fragment gets called and so on.

我不了解这种行为.请帮忙!

I don't understand this behavior. Please help!

推荐答案

默认情况下为viewpager.setOffscreenPageLimit(1),这意味着默认情况下,View分页器将在当前选项卡的右侧选项卡上至少加载1个,在左侧选项卡上加载一个.

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.

之所以这样做,主要是因为在滑动Viewpager时有一点,两个选项卡的某些区域都可见.对于那些平稳的过渡,需要预加载.

It is done so, mostly because there is a point when you slide viewpager, when certain area of both tabs is visible. For those smooth transitions preloading is required.

无法对其进行设置viewpager.setOffscreenPageLimit(0).

唯一的出路是使用此方法setUserVisibleHint 将此添加到您的片段中

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 you can call all loading function.

检查示例示例

这篇关于第二个片段(选项卡)的onCreateView在第一个片段上被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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