Giter Site home page Giter Site logo

lazyfragment's Introduction

LazyFragment Build Status

类似案例:

微信 网易新闻

解决问题:

在多个Fragment需要加载的时候,启动速度往往会变慢.
分析会发现并非所有的Fragment都需要第一时间将数据填充完毕.
因为它们都还没有被用户所"看见".
所以我们要达到的效果是Fragment被显示后才加载数据(lazy load).
LazyFragment由此诞生....鼓掌~~~~~~~~~~

例:
Toolbar + ViewPager + Fragment

github

使用说明:

  • extends BaseFragment
    其他生命周期的方法需要重写 就自己overwrite

两个方法重点说明:

  • initViews(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);
    抽象方法
    与 onCreateView 类似.
    initViews 是只要 Fragment 被创建就会执行的方法.
    也就是说如果我们不想用 LazyLoad 模式
    则把所有的初始化 和 加载数据方法都写在 initViews 即可.

  • initData();
    抽象方法
    若将代码写在initData中,则是在Fragment真正显示出来后才会去Load(懒加载).

注意事项:

sample很简单.代码注释也写的很清楚
有个FragmentTransaction的坑,在BaseFragment文件注释中有说明(注2部分)
这里还是贴出来吧
/**
 * <pre>
 * 若把初始化内容放到initData实现
 * 就是采用Lazy方式加载的Fragment
 * 若不需要Lazy加载则initData方法内留空,初始化内容放到initViews即可
 *
 * 注1:
 * 如果是与ViewPager一起使用,调用的是setUserVisibleHint。
 *
 * 注2:
 * 如果是通过FragmentTransaction的show和hide的方法来控制显示,调用的是onHiddenChanged.
 * 针对初始就show的Fragment 为了触发onHiddenChanged事件 达到lazy效果 
 * 需要先hide再show
 * eg:
 * transaction.hide(aFragment);
 * transaction.show(aFragment);
 *
 * Created by Mumu
 * on 2015/11/2.
 * </pre>
 */

链接

1.https://github.com/xmagicj/LazyFragment

lazyfragment's People

Contributors

xmagicj avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.