问题描述
我有一个有 2 列的交错网格.这是有效的.我想要的是在位置 0 使行跨越 2 列.我之前使用 GridLayoutManger 很容易地做到了这一点:
I have a staggered grid that has 2 columns. This is working. What I want is at position 0 for the row to span across the 2 columns. I have done this before quite easily using GridLayoutManger as so:
mGridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return position == 0 ? 2 : 1;
}
});
StaggeredGridLayoutManager 不像 GridLayoutManager 那样为我提供这个功能.
StaggeredGridLayoutManager doesn't provide me with this functionality like GridLayoutManager does.
有没有不同的方法来做到这一点?我已经搜索过但没有发现任何有同样问题的人,这令人惊讶,因为我认为当 ProgressBar 显示在 RecyclerView 的最后一行时,此功能对于我的场景和无限滚动非常有用.
Is there a different way of doing this? I have searched but not found anyone with the same problem, which is surprising as I would think this functionality would be quite useful for my scenario and for infinite scrolling, when a ProgressBar is shown in the last row of the RecyclerView.
推荐答案
您可以使用 setFullSpan 方法.
通过这种方式,项目将使用所有跨度区域进行布局.
You can use the setFullSpan method.
In this way the item will layout using all span area.
这意味着,如果方向是垂直的,则视图将具有全宽;如果方向是水平的,则视图将具有全高.
像这样:
public final void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) viewHolder.itemView.getLayoutParams();
layoutParams.setFullSpan(true);
}
注意.
它支持跨越所有列的视图,但对于您的情况应该足够了.
Pay attention.
It supports views that span all the columns, but it should be enough for your case.
这篇关于在 StaggeredGridLayoutManager 中设置单行的跨度大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!