【问题标题】:Gridlayout manager with different column numbers in each row每行中具有不同列号的 Gridlayout 管理器
【发布时间】:2018-04-14 08:20:16
【问题描述】:

我需要一个具有不同列数的回收站视图。为此,我使用了具有动态跨度大小的网格布局管理器。这是我的代码:

rvSubCat.layoutManager = StaggeredGridLayoutManager(4, 1)
        val viewTreeObserver = rvSubCat.getViewTreeObserver()
        viewTreeObserver.addOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener { calculateSize() })
        categoryAdapter = CategoryAdapter(categoryList, true, this)
        rvSubCat.adapter = categoryAdapter


 private fun calculateSize() {
        val spanCount = Math.floor((rvSubCat.getWidth() / SizeUtils.dp2px(sColumnWidth.toFloat())).toDouble()).toInt()
        (rvSubCat.getLayoutManager() as StaggeredGridLayoutManager).spanCount = spanCount
    }

但是想要的结果并不好。它正在固定每行中的列数。我需要的列数应该取决于行项的总宽度。

需要这个(期望的结果):

得到这个

【问题讨论】:

  • 我认为你应该为此使用标签视图库

标签: android android-recyclerview gridlayoutmanager


【解决方案1】:

我使用的最好的轻量级库

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

好处是你不需要改变你现有的代码,你只需要改变 recyclerview 的 LayoutManager 就像

recyclerView.setLayoutManager(new FlowLayoutManager());

一切准备就绪,快乐编码:)

更新您已经知道如何填充回收站视图。但是通过以下方式验证

private void setAdapter() {
    recyclerView = (RecyclerView) fragment_view.findViewById(R.id.rv);
    recyclerView.setLayoutManager(new FlowLayoutManager());
    myRecyclerAdapter = new AdapterOrders(getActivity(), list);
    recyclerView.setAdapter(myRecyclerAdapter);
}

从任何来源更改列表后,请致电myRecyclerAdapter.notifyDataSetChanged();

【讨论】:

  • 很好的解决方案。但是回收站视图变成了空白 :( 现在没有数据可见
  • 它正在使用:flowLayoutManager.isAutoMeasureEnabled = true
  • 好家伙,我认为当您的项目高度或宽度与父项匹配时会发生此问题
【解决方案2】:

您需要的是流式布局。我认为这些库将帮助您解决目的:

https://github.com/nex3z/FlowLayout
https://github.com/ApmeM/android-flowlayout

根据文本大小进行调整。希望这会有所帮助!

【讨论】:

  • 很好的解决方案,但我有一个回收站视图要显示。我可以在回收站视图中添加流程布局吗
  • 是的,你可以,但是看到问题中的图像我猜你不需要回收站视图,流布局会自行处理,你只需要动态添加标签。
  • 我已经用我在项目中使用的另一个库更新了答案。
猜你喜欢
  • 2018-10-03
  • 1970-01-01
  • 1970-01-01
  • 2015-12-05
  • 1970-01-01
  • 2011-05-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多