首页 > 解决方案 > RecyclerView 无法应用于 FlexboxLayoutManager

问题描述

我正在尝试使用FlexboxLayoutManager,我按照教程进行操作,但遇到了这个问题:

RecyclerView中的setLayoutManager(android.support.v7.widget.RecyclerView.LayoutManager)不能应用到(com.google.android.flexbox.FlexboxLayoutManager)

这是我的代码:

  FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(FreightResultActivity.this);
  layoutManager.setFlexDirection(FlexDirection.COLUMN);
  layoutManager.setJustifyContent(JustifyContent.FLEX_END);
  recyclerView.setLayoutManager(layoutManager);

标签: androidandroid-recyclerview

解决方案


如果您使用的是最新版本的 Flexbox (1.1.0),它会使用 AndroidX,因此FlexboxLayoutManager实际 extendsandroidx.recyclerview.widget.RecyclerView.LayoutManager可能会导致此异常。尝试迁移到androidx;androidx.recyclerview:RecyclerView:1.0.0用作 recyclerview 依赖项应该可以解决您的问题。Android Studio 在“重构”下具有“迁移到 AndroidX”功能,您可以使用它来进行迁移。


推荐阅读