scwang90/SmartRefreshLayout · error · RuntimeException

最多只支持3个子View,Most only support three sub view

Error message

最多只支持3个子View,Most only support three sub view

What it means

SmartRefreshLayout.onFinishInflate() throws immediately when the XML layout declares more than 3 direct children of the <com.scwang.smart.refresh.layout.SmartRefreshLayout> tag. The widget reserves its child slots for exactly one header, one content view, and one footer, so any fourth direct child is a layout-authoring error and fails fast at inflate time rather than silently misbehaving.

Source

Thrown at refresh-layout-kernel/src/main/java/com/scwang/smart/refresh/layout/SmartRefreshLayout.java:347

            mEnableLoadMore = true;
        }

        ta.recycle();
    }
    //</editor-fold>

    //<editor-fold desc="生命周期 life cycle">

    /**
     * 重写 onFinishInflate 来完成 smart 的特定功能
     * 1.智能寻找 Xml 中定义的 Content、Header、Footer
     */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        final int count = super.getChildCount();
        if (count > 3) {
            throw new RuntimeException("最多只支持3个子View,Most only support three sub view");
        }

        int contentLevel = 0; // 当前找到内容布局的级别(0还没找到 1普通内容 2可滚动内容)
        int indexContent = -1; // 内容布局所在的序号
        for (int i = 0; i < count; i++) {
            View view = super.getChildAt(i);
            if (SmartUtil.isContentView(view) && (contentLevel < 2 || i == 1)) {
                // 可滚动内容
                indexContent = i;
                contentLevel = 2; // 标记为可滚动内容,不会再被替换
            } else if (!(view instanceof RefreshComponent) && contentLevel < 1) {
                // 普通内容
                indexContent = i;
                contentLevel = i > 0 ? 1 : 0; // 如果是第一个标记为:未找到(第一个有可能是自定义Header),否则标记为:普通内容
            }
        }

        int indexHeader = -1;

View on GitHub (pinned to 224db48f8a)

Solutions

  1. Keep exactly one content child: wrap all your content views (toolbar, RecyclerView, overlays) in a single FrameLayout/CoordinatorLayout inside SmartRefreshLayout.
  2. Verify in the layout XML that direct children are only: optional header, one content view, optional footer.
  3. Headers/footers that need multiple views should be a single custom view implementing RefreshHeader/RefreshFooter, not multiple children.
  4. Use RefreshHeader/RefreshFooter classes or ClassicsHeader/ClassicsFooter for header/footer slots instead of extra plain views.

Example fix

<!-- before -->
<SmartRefreshLayout ...>
    <TextView .../>      <!-- header-ish -->
    <RecyclerView .../>
    <TextView .../>      <!-- footer-ish -->
    <ProgressBar .../>   <!-- 4th child -> crash -->
</SmartRefreshLayout>

<!-- after -->
<SmartRefreshLayout ...>
    <RecyclerView .../>  <!-- the single content view -->
    <FrameLayout ...>    <!-- wrap overlays inside a content container -->
        <ProgressBar .../>
    </FrameLayout> <!-- NO: still 2 children; instead put ProgressBar INSIDE the content or use footer slot -->
</SmartRefreshLayout>
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Placing 4+ views directly inside <SmartRefreshLayout> in XML, e.g. two content views, a header, and a footer, or putting a toolbar plus a RecyclerView plus extra banners as siblings instead of wrapping them in a single content ViewGroup.

Common situations: Developers new to SmartRefreshLayout treating it like a FrameLayout/LinearLayout and stacking multiple children; refactoring a layout and forgetting to wrap non-refresh content into the single content container; adding a loading overlay or empty-state view as a direct child of the refresh layout.

Related errors


AI-assisted analysis of scwang90/SmartRefreshLayout@224db48f8a (2026-08-14). Data as JSON: /api/errors/0fe43562c31cffbf. Report an issue: GitHub.