Git Product home page Git Product logo

smartfragmentpager's Introduction

README

###需要解决的问题

使用ViewPager做多个页面Fragment视图时,最小的缓存数量只能是1,即使复写了ViewPager将最小缓存数量降低到0,新划出的界面也是黑色的,并不符合一般的设计需求。

如果被缓存的Fragment中有大量的数据查询或网络请求等耗费资源的操作,那么必然造成了不必要的资源损耗,如果用户根本没有打算滑动到这个界面,那么预先加载的数据完全是浪费

###解决方案

在ViewPager滑动的过程中,其Adapter会被委托处理子Fragment事件,而每次滑动结果产生,Adapter都会通过调用Fragment的setUserVisibleHint来告知Fragment其当前是否真正展现在用户眼前。所以通过复写Fragment的setUserVisibleHint方法,再匹配以适当的逻辑处理就可以达到我们想要的效果——只有当Fragment真正可视时才加载数据。

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser && isResumed()) {
        //做数据加载
    }
}

不要忽略了isResumed,因为setUserVisibleHint被调用的时候可能Fragment并没有attach到视图上。一般这种情况会出现在第一个Fragment中,可以通过识别position为0解决,具体可查看代码。

smartfragmentpager's People

Contributors

lkv1988 avatar

Stargazers

TangGSen avatar Randhika 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.