代码之家  ›  专栏  ›  技术社区  ›  Dnyanesh M

有没有办法根据Android中RecyclerView的项目文本长度为每行设置不同的“spanCount”

  •  3
  • Dnyanesh M  · 技术社区  · 7 年前

    实际上,我正在为我当前的应用程序设计下面的页面。

    Expected Screen

    如果仔细观察上面的屏幕截图,您将看到 Span-Count 在每一行中都是不同的(有时是2/3/1),似乎这些项目是根据各自的 item text-length 或 Item-Width RecyclerView StaggeredGridLayoutManager

    下面是我的代码:

    rvCategory = (RecyclerView) findViewById(R.id.rvCategory);
    RecyclerView.LayoutManager mLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
    rvCategory.addItemDecoration(new SpacesItemDecoration(9, 15));
    rvCategory.setLayoutManager(mLayoutManager);
    

    使用上面的代码,我的屏幕如下所示--

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  1
  •   letsintegreat israelss    7 年前

    你需要使用 FlowLayoutManager

    implementation 'com.xiaofeng.android:flowlayoutmanager:1.2.3.2'
    

    以及 RecyclerView

    FlowLayoutManager flowLayoutManager = new FlowLayoutManager();
    flowLayoutManager.setAutoMeasureEnabled(true);
    recyclerView.setLayoutManager(flowLayoutManager);