简书链接:腾讯bugly文章fragment懒加载代码
文章字数:596,阅读全文大约需要2分钟
原文: https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzA3NTYzODYzMg==&scene=124&#wechat_redirect
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| public class LazyFragment extends Fragment { private View mRootView; private boolean mIsInited; private boolean mIsPrepared; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mRootView = inflater.inflate(R.layout.fragment_lazy, container, false); mIsPrepared = true; lazyLoad(); return mRootView; } public void lazyLoad() { if (getUserVisibleHint() && mIsPrepared && !mIsInited) { //异步初始化,在初始化后显示正常UI loadData(); } } private void loadData() { new Thread() { public void run() { //1. 加载数据 //2. 更新UI //3. mIsInited = true } }.start(); } @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser) { lazyLoad(); } } public static LazyFragment newInstance() { return new LazyFragment(); } }
|
注意点:
在Fragment中有两个变量控制是否需要做数据加载:
mIsPrepared:表示UI是否准备好,因为数据加载后需要更新UI,如果UI还没有inflate,就不需要做数据加载,因为setUserVisibleHint()会在onCreateView()之前调用一次,如果此时调用,UI还没有inflate,因此不能加载数据。
mIsInited:表示是否已经做过数据加载,如果做过了就不需要做了。因为setUserVisibleHint(true)在界面可见时都会调用,如果滑到该界面做过数据加载后,滑走,再滑回来,还是会调用setUserVisibleHint(true),此时由于mIsInited=true,因此不会再做一遍数据加载。
lazyLoad():懒加载的核心类,在该方法中,只有界面可见(getUserVisibleHint()==true)、UI准备好(mIsPrepared==true)、过去没做过数据加载(mIsInited==false)时,才需要调loadData()做数据加载,数据加载做完后把mIsInited置为true。
布局XML主要分两个container,一个是初始显示的状态,即R.id.container_empty,当数据加载完成,就显示R.id.container:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<RelativeLayout android:id="@+id/container_empty" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="正在加载" />
</RelativeLayout> <RelativeLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone" > ... </RelativeLayout> </FrameLayout>
|
最后附上我改的懒加载类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| package cn.qssq666.quickdevelopframe.base.fragment;
import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;
/** * Created by qssq on 2017/11/14 qssq666@foxmail.com */
public abstract class LazyFragment extends Fragment { public View getRootView() { return mRootView; }
private View mRootView; private boolean mIsInited; private boolean mIsPrepared;
public View onCreateViewFix(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(getLayoutID(), container, false);
}
@Override public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mRootView = onCreateViewFix(inflater, container, savedInstanceState); mIsPrepared = true; lazyLoad(inflater, container); return mRootView; }
public void lazyLoad(LayoutInflater inflater, ViewGroup container) { if (getUserVisibleHint() && mIsPrepared && !mIsInited) { //异步初始化,在初始化后显示正常UI init(inflater, container); mIsInited = true; } }
//1. 加载数据 //2. 更新UI //3. mIsInited = true
public abstract void init(LayoutInflater inflater, ViewGroup container);
@Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser) { lazyLoad(null, null); } }
@Override public void onDestroyView() { super.onDestroyView(); if (getRootView() != null) { ViewGroup viewGroup = ((ViewGroup) getRootView()); if (viewGroup != null) { viewGroup.removeView(getRootView()); } }
}
protected abstract int getLayoutID(); }
|
懒加载有一个严重的bug,我至今没搞出来,第一个fragment 会空白,第一次不会,再次切换的时候空白了.
参考文献
入门(https://www.raywenderlich.com/169885/android-fragments-tutorial-introduction-2)
教程1
(http://assets.en.oreilly.com/1/event/68/Fragments%20for%20All%20Presentation.pdf)
教程2
(http://vinsol.com/blog/2014/09/15/advocating-fragment-oriented-applications-in-android/)
生命周期(https://github.com/xxv/android-lifecycle)
detach vs remove
(https://stackoverflow.com/questions/9156406/whats-the-difference-between-detaching-a-fragment-and-removing-it)
Google I/O 2016: What the Fragment?(https://www.youtube.com/watch?v=k3IT-IJ0J98)
Google I/O 2017: Fragment Tricks
(https://www.youtube.com/watch?v=eUG3VWnXFtg)
mAdded和mActive的区别(https://stackoverflow.com/questions/25695960/difference-between-madded-mactive-in-source-code-of-support-fragmentmanager)
如何避免IllegalStateException异常(http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html)