【问题标题】:Staggered grid layout manager dynamic number of columns交错网格布局管理器动态列数
【发布时间】:2017-01-24 16:39:45
【问题描述】:

如何使用StaggeredGridLayoutManagerRecyclerView 中创建动态列数。我已经在工作RecyclerView。这是我需要做的屏幕:

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

    如果最大列是两个,那将非常简单。对于占据整个宽度的视图,setFullSpan(true) 就像在 Recyclerview Adapter 的 BindView 方法中一样

    //rlm is RecyclerView.LayoutManager passed in constructor or setter in adapter 
    if (rlm instanceof StaggeredGridLayoutManager) {
       StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
        /*
         * to make View to occupy full width of the parent
         */
         layoutParams.setFullSpan(true);
    }
    

    【讨论】:

    • 亲爱的@Ravi Teja rlm 是什么?
    • 布局管理器的实例,您可以在适配器的构造函数中传递,在适配器中有一个setter方法
    • 如果你有 3 个 cols 怎么办?
    • @Shine 它适用于 n 列。您只能使元素占据整个宽度。
    • 是的。截至目前,StaggeredGridLayoutManager 不如 GridLayoutManager 灵活