首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据项目位置动态更改StaggeredGridLayout的列跨度计数

根据项目位置动态更改StaggeredGridLayout的列跨度计数
EN

Stack Overflow用户
提问于 2019-07-01 17:33:54
回答 3查看 1.6K关注 0票数 3

我想为(n)个项目列表实现这个布局:

到目前为止,我可以在两列中显示项目,但是如何将两列合并到一个特定位置(即作为项目7)?

我已经尝试了许多在SO和其他地方提供的解决方案,但没有一个对我有效。那里提供的解决方案要么是使用LinearLayout膨胀静态布局,要么是为列中的分区提供解决方案。

我也尝试过使用setSpanLookup方法,但没有成功。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-07-01 18:49:04

最后,在我竭尽全力之后,我终于找到了解决方案:

代码语言:javascript
复制
    @Override
        public void onViewAttachedToWindow(MyViewHolder holder) {
            super.onViewAttachedToWindow(holder);
            ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
            if(lp != null
                    && lp instanceof StaggeredGridLayoutManager.LayoutParams
                    && (holder.getLayoutPosition() == 6 || holder.getLayoutPosition() == 13)) {
                StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
                p.setFullSpan(true);
            }
           holder.setIsRecyclable(false);
        }

希望这也能对你有所帮助。

票数 3
EN

Stack Overflow用户

发布于 2019-07-01 17:54:18

正如你所说,我认为setSpanLookup是最好的方法。

尝试以下代码:

代码语言:javascript
复制
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val layoutManager = GridLayoutManager(this, 2)
        layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                if (position == 6 || position == 13) return 2
                else return 1
            }
        }
        rv.layoutManager = layoutManager

        val items = mutableListOf<String>()
        for (i in 1..14) {
            items.add("item$i")
        }
        rv.adapter = MyAdapter(items)
    }
}

您需要知道getSpanSize返回的值表示权重,而不是列计数。

在项目布局方面,您可以在特殊位置使用不同的ViewHolder。

例如:

代码语言:javascript
复制
companion object{
    const val NORMAL = 0
    const val WIDE = 1
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
    ...
    if (viewType == WIDE) return WideViewHolder(wideView)
    else return NormalViewHolder(normalView)
}

override fun getItemViewType(position: Int): Int {
    if (position == 6 || position == 13) {
        return WIDE
    }
    return NORMAL
}
票数 0
EN

Stack Overflow用户

发布于 2019-07-01 17:56:04

只需创建一个表格:在您的示例中是8行3列,设置边框和边框对齐,第7项和第14项将采用3个单元格。下面是一个示例:

代码语言:javascript
复制
private void insertCell(PdfPTable table, String text, int align, int colspan, Font font) {

    //create a new cell with the specified Text and Font
    PdfPCell cell = new PdfPCell(new Phrase(text.trim(), font));
    //set the cell alignment
    cell.setHorizontalAlignment(align);
    //set the cell column span in case you want to merge two or more cells
    cell.setColspan(colspan);
    //in case there is no text and you wan to create an empty row
    if (text.trim().equalsIgnoreCase("")) {
        cell.setMinimumHeight(10f);
    }
    //add the call to the table
    table.addCell(cell);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56833225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档