ViewPager Fragment 禁止预加载数据

本文介绍如何优化使用ViewPager和Fragment的应用程序,通过合理利用setUserVisibleHint方法减少不必要的数据加载和网络请求,降低内存消耗,提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        最近项目做优化,主页面用的是viewpager + Fragment 实现。发现每次启动应用后,就会有其他页面会发起接口请求,这点很不好。虽然说可以提前加载其他页面的数据,但是会在某一短时间内发起很多的请求,加大内存的消耗。同时,这样也不合使用习惯,页面还没有看见就发起了请求,加载了页面。

        最后解决办法使用Fragment 的 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.
     *
     * <p>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.</p>
     *
     * @param isVisibleToUser true if this fragment's UI is currently visible to the user (default),
     *                        false if it is not.
     */
</pre><pre name="code" class="html">   <pre name="code" class="html"> @Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		if (isVisibleToUser) {// onResume
			mIsResume = true;
		    isVisible = true;
			lazyLoad();

		} else {// onPause
			isVisible = false;
		}
	}
 
    public abstract void lazyLoad();


       /**
     * 标志位,标志已经初始化完成
     */
    private boolean isPrepared;
    /**
     * 是否已被加载过一次,第二次就不再去请求数据了
     */
    private boolean mHasLoadedOnce;
</pre><pre name="code" class="html">    <pre name="code" class="html">public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_base_listview, null);
        isPrepared = true;
        return view;
    }

@Override
    public void lazyLoad() {
        super.lazyLoad();

        LogUtils.d(TAG, "lazyload isPrepared = " + isPrepared + " isVisible == " + isVisible + " mHasLoadedOnce == " + mHasLoadedOnce);
        if (!isPrepared || !isVisible || mHasLoadedOnce) {
            return;
        }
        if (null != mAdapter && mAdapter.getCount() == 0 && mLoadOver && needRequest()) {
            LogUtils.d(TAG, " initData--->");
            mPtrFrame.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mHasLoadedOnce = true;
                    mPtrFrame.autoRefresh();
                }
            }, 100);
        }

    }
    由于页面的嵌套很深, 还遇到了一种情况。项目最外层结构是Viewpager + Fragment 。 但是在一个Fragment 中又有 FragmentA, FragmentB。 其中FragmentA 包含 Viewpager + Fragment。 这里面子FragmentC 还包含两种样式的布局。在这种情况下,最里层Fragment的 <span style="font-family: Arial, Helvetica, sans-serif;">getUserVisibleHint()为false。但是我们可以通过判断parentFragment的状态还判断子Fragment的。</span>
<span style="font-family: Arial, Helvetica, sans-serif;">       </span><pre name="code" class="html"> android.support.v4.app.Fragment parentFragment =  getParentFragment();
        if(parentFragment != null){
            if(parentFragment.getUserVisibleHint()){
                isVisible = true;
                LogUtils.d(TAG, " isVisible = true");
            }
        }



当activity 里加载Fragment ,此时判断fragment 的 getUserVisibleHint()为false, 需要手动的设为true。

    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值