在StaggeredGridLayoutManager中设置单行的跨度大小 [英] Setting span size of single row in StaggeredGridLayoutManager
问题描述
我有一个交错的网格,其中有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.
推荐答案
You can use the setFullSpan method.
In this way the item will layout using all span area.
这意味着,如果方向是垂直的,则视图将具有完整宽度;如果方向是水平的,则视图将具有完整的高度.
That means, if orientation is vertical, the view will have full width; if orientation is horizontal, the view will have full height.
类似这样的东西:
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中设置单行的跨度大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!