Git Product home page Git Product logo

Comments (6)

vangeldu avatar vangeldu commented on September 23, 2024

recyclerview父层是 swipeRefreshLayout

from loadsir.

KingJA avatar KingJA commented on September 23, 2024

@duweigang 请参考我的Demo:LoadSirBestPractice,和你描述的差不多场景。如果还有疑问我们再详细谈论。

from loadsir.

vangeldu avatar vangeldu commented on September 23, 2024

已经找到原因:关于swipeRefreshLayout嵌套recyclerview的问题时,loadLayout宽高为0的原因。

原因是我在register(recyclerview)【之前】,先行调用了swipeRefreshLayout.setColorSchemeColors(Color.parseColor("#893712"));这样一段代码。
而在如上方法的源码调用了如下方法:

private void ensureTarget() {
        // Don't bother getting the parent height if the parent hasn't been laid
        // out yet.
        if (mTarget == null) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                if (!child.equals(mCircleView)) {
                    mTarget = child;
                    break;
                }
            }
        }
    }

其中主要是确定了mTarget指向了recyclerview。

之后我调用register(recyclerview),然后按照View绘制流程,执行到SwipeRefreshLayout.onMeasure()中有这么一段代码:

mTarget.measure(MeasureSpec.makeMeasureSpec(
               getMeasuredWidth() - getPaddingLeft() - getPaddingRight(),
               MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
               getMeasuredHeight() - getPaddingTop() - getPaddingBottom(), MeasureSpec.EXACTLY));

因为此时的target是recyclerview,会计算recyclerview的宽高,但是因为SwipeRefreshLayout认为mTarget才是他的child,所以,之后在register了recyclerview之后,swipeRefreshLayout的mTarget按照正常逻辑应该是指向loadlayout,但是实际情况是继续指向recyclerview,整个过程loadlayout都没有执行过他自己的measure方法,所以宽高才是0。

当然如果不调用setColorSchemeColors这个方法,其实register和view的初始化调用顺序怎么写都没事,因为在绘制时,register之后是会指向正确的loadlayout。这里之所以会有问题,也是和SwipeRefreshLayout这个控件的measure方法实现有关。
可能有分析不到位的地方。。。(说这些是希望后来者别被这个调用顺序坑到),感觉作者可以加些说明关于调用的顺序方面。

from loadsir.

KingJA avatar KingJA commented on September 23, 2024

@duweigang 感谢你提供详细原因:smiley: ,我会将你的宝贵经验放入FAQ中。

from loadsir.

vangeldu avatar vangeldu commented on September 23, 2024

@KingJA 不客气,感谢作者对开源做出的贡献

from loadsir.

KingJA avatar KingJA commented on September 23, 2024

@duweigang Thanks a lot. 欣慰.

from loadsir.

Related Issues (20)

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.