在 StaggeredGridLayoutManager 中设置单行的跨度大小
Setting span size of single row in StaggeredGridLayoutManager
我有一个包含 2 列的交错网格。这是工作。我想要的是在位置 0 处让该行跨越 2 列。我之前使用 GridLayoutManger 很容易地做到了这一点:
mGridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return position == 0 ? 2 : 1;
}
});
StaggeredGridLayoutManager 没有像 GridLayoutManager 那样为我提供此功能。
有没有其他方法可以做到这一点?我已经搜索但没有发现任何人遇到同样的问题,这令人惊讶,因为我认为当 RecyclerView 的最后一行显示 ProgressBar 时,此功能对我的场景和无限滚动非常有用。
您可以使用setFullSpan方法。
这样,该项目将使用所有跨度区域进行布局。
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);
}
关注
它支持跨越所有列的视图,但对于您的情况应该足够了。
正如@Daniele Segato 所说,
Since the ViewHolder should not be changed and we have to keep onBindViewHolder lean., It is better to set the isFullSpan parameter in the onCreateViewHolder method. This LayoutParamter is for the parent View, namely, the RecylcerView, to decide how to layout the children views.
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StaggeredProductCardViewHolder {
var isFullSpan = false
var layoutId = R.layout.shr_staggered_product_card_first
if (viewType == 1) {
layoutId = R.layout.shr_staggered_product_card_second
} else if (viewType == 2) {
layoutId = R.layout.shr_staggered_product_card_third
isFullSpan = true
}
val layoutView = LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
(layoutView.layoutParams as StaggeredGridLayoutManager.LayoutParams).isFullSpan= isFullSpan
return StaggeredProductCardViewHolder(layoutView)
}
Final Result: Don't use gridLayoutManger's SpanCoutLookUp Method
致所有使用 Kotlin 的人。
在 onBindViewHolder 中使用 isFullSpan 有效。
override fun onBindViewHolder(holder: LoadStateViewHolder, loadState: LoadState) {
holder.bind(loadState)
val layoutParams = holder.itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams
layoutParams.isFullSpan = true
}
我有一个包含 2 列的交错网格。这是工作。我想要的是在位置 0 处让该行跨越 2 列。我之前使用 GridLayoutManger 很容易地做到了这一点:
mGridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return position == 0 ? 2 : 1;
}
});
StaggeredGridLayoutManager 没有像 GridLayoutManager 那样为我提供此功能。
有没有其他方法可以做到这一点?我已经搜索但没有发现任何人遇到同样的问题,这令人惊讶,因为我认为当 RecyclerView 的最后一行显示 ProgressBar 时,此功能对我的场景和无限滚动非常有用。
您可以使用setFullSpan方法。
这样,该项目将使用所有跨度区域进行布局。
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);
}
关注
它支持跨越所有列的视图,但对于您的情况应该足够了。
正如@Daniele Segato 所说,
Since the ViewHolder should not be changed and we have to keep onBindViewHolder lean., It is better to set the isFullSpan parameter in the onCreateViewHolder method. This LayoutParamter is for the parent View, namely, the RecylcerView, to decide how to layout the children views.
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StaggeredProductCardViewHolder {
var isFullSpan = false
var layoutId = R.layout.shr_staggered_product_card_first
if (viewType == 1) {
layoutId = R.layout.shr_staggered_product_card_second
} else if (viewType == 2) {
layoutId = R.layout.shr_staggered_product_card_third
isFullSpan = true
}
val layoutView = LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
(layoutView.layoutParams as StaggeredGridLayoutManager.LayoutParams).isFullSpan= isFullSpan
return StaggeredProductCardViewHolder(layoutView)
}
Final Result: Don't use gridLayoutManger's SpanCoutLookUp Method
致所有使用 Kotlin 的人。
在 onBindViewHolder 中使用 isFullSpan 有效。
override fun onBindViewHolder(holder: LoadStateViewHolder, loadState: LoadState) {
holder.bind(loadState)
val layoutParams = holder.itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams
layoutParams.isFullSpan = true
}