GithubHelp home page GithubHelp logo

lshjune / lazyfragment Goto Github PK

View Code? Open in Web Editor NEW

This project forked from xmagicj/lazyfragment

0.0 2.0 0.0 1.19 MB

:octocat: 类似微信,网易新闻 延迟加载Fragment基类 , 支持与ViewPager组合刷新全部Fragment

Home Page: https://github.com/xmagicj/LazyFragment

Java 100.00%

lazyfragment's Introduction

LazyFragment Build Status

类似案例:

微信 网易新闻

解决问题 1:

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

例:
Toolbar + ViewPager + Fragment

github

解决问题 2:

ViewPager + PagerAdapter需要刷新所有子Fragment的场景.
不要new 新的 PagerAdapter 而采取reset数据的方式.
所以要求Fragment重新走initData方法.
具体参见代码示例 
**搜索关键词 setForceLoad / refreshAllFragment / refreshData 即可找到关键代码~**

github  

使用说明:

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

三个方法重点说明:

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

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

  • setForceLoad();
    忽略isFirstLoad的值,强制刷新数据,前提是Visible & Prepared.
    未Visible & Prepared的页面需要注意在RefreshData的时候视图为空的问题, 具体请参见实例代码
    搜索关键词
    setForceLoad / refreshAllFragment / refreshData 即可找到关键代码

注意事项(一定要看完):


有个FragmentTransaction的坑,在BaseFragment文件注释中有说明(注2部分)
/**
 * 注2:
 * 如果是通过FragmentTransaction的show和hide的方法来控制显示,调用的是onHiddenChanged.
 * 针对初始就show的Fragment 为了触发onHiddenChanged事件 达到lazy效果 
 * 需要先hide再show
 * 需要先hide再show
 * 需要先hide再show
 * eg:
 * transaction.hide(aFragment);
 * transaction.show(aFragment);
 */

链接

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

lazyfragment's People

Contributors

xmagicj avatar

Watchers

James Cloos avatar  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.