{"record":{"id":"09e56fe4e63cc450","repo":"youth5201314/banner","slug":"the-layoutmanager-must-be-linearlayoutmanager","errorCode":null,"errorMessage":"The layoutManager must be LinearLayoutManager","messagePattern":"The layoutManager must be LinearLayoutManager","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"banner/src/main/java/com/youth/banner/itemdecoration/MarginDecoration.java","lineNumber":39,"sourceCode":"    @Override\n    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent,\n                               @NonNull RecyclerView.State state) {\n        LinearLayoutManager linearLayoutManager = requireLinearLayoutManager(parent);\n        if (linearLayoutManager.getOrientation() == LinearLayoutManager.VERTICAL) {\n            outRect.top = mMarginPx;\n            outRect.bottom = mMarginPx;\n        } else {\n            outRect.left = mMarginPx;\n            outRect.right = mMarginPx;\n        }\n    }\n\n    private LinearLayoutManager requireLinearLayoutManager(@NonNull RecyclerView parent) {\n        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();\n        if (layoutManager instanceof LinearLayoutManager) {\n            return (LinearLayoutManager) layoutManager;\n        }\n        throw new IllegalStateException(\"The layoutManager must be LinearLayoutManager\");\n    }\n}\n","sourceCodeStart":21,"sourceCodeEnd":42,"githubUrl":"https://github.com/youth5201314/banner/blob/777c7dbfaa0a48d78101858d5520f695fef91fbd/banner/src/main/java/com/youth/banner/itemdecoration/MarginDecoration.java#L21-L42","documentation":"MarginDecoration (a RecyclerView ItemDecoration) only works when the RecyclerView's LayoutManager is a LinearLayoutManager (or subclass like GridLayoutManager). During getItemOffsets it calls requireLinearLayoutManager, which throws IllegalStateException if the LayoutManager is missing or of another type (e.g. StaggeredGridLayoutManager). This is a precondition check so the decoration can compute margins from the linear orientation.","triggerScenarios":"Attaching MarginDecoration (directly or via banner.setItemCountAndDecoration-style usage) to a RecyclerView whose LayoutManager is StaggeredGridLayoutManager, GridLayoutManager with a non-LinearLayoutManager cast path, a custom LayoutManager, or is null (no LayoutManager set on the RecyclerView).","commonSituations":"Using the banner/indicator with a custom RecyclerView setup and forgetting setLayoutManager; switching LayoutManager types after adding the decoration; applying the decoration to a generic RecyclerView rather than the banner's internal one; code migration where a Flexbox/StaggeredGrid layout replaced a LinearLayoutManager.","solutions":["Call recyclerView.setLayoutManager(new LinearLayoutManager(context)) (or set orientation via BannerConfig) before adding MarginDecoration.","Remove MarginDecoration if you intentionally use a non-linear LayoutManager; it is not designed for it.","If wrapping a banner-internal RecyclerView, let the Banner manage its own LinearLayoutManager instead of replacing it.","Check that the LayoutManager is set before Layout/adapter attach, since parent.getLayoutManager() can be null early."],"exampleFix":"// before\nrecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));\nrecyclerView.addItemDecoration(new MarginDecoration(...)); // IllegalStateException\n// after\nrecyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));\nrecyclerView.addItemDecoration(new MarginDecoration(...));","handlingStrategy":"type-guard","validationCode":"if (!(recyclerView.getLayoutManager() instanceof LinearLayoutManager)) {\n    recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));\n}\nrecyclerView.addItemDecoration(new MarginDecoration(context));","typeGuard":"RecyclerView.LayoutManager lm = recyclerView.getLayoutManager();\nboolean ok = lm instanceof LinearLayoutManager;\nLinearLayoutManager llm = ok ? (LinearLayoutManager) lm : null;","tryCatchPattern":null,"preventionTips":["Always call setLayoutManager before addItemDecoration on the RecyclerView.","Only use MarginDecoration with linear layouts; pick a different decoration for grids/staggered layouts.","Don't replace the Banner's internal LayoutManager with a custom one.","Re-check LayoutManager type after any layout refactor or migration."],"tags":["android","recyclerview","layoutmanager","banner"],"backgroundTag":"invalid-argument-value","analyzedSha":"777c7dbfaa0a48d78101858d5520f695fef91fbd","analyzedAt":"2026-09-08T00:03:24.747Z","contentChangedAt":"2026-09-08T00:03:24.747Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}