gpt4 book ai didi

android - 为 GridLayoutManager 设置完整跨度

转载 作者:太空狗 更新时间:2023-10-29 15:59:38 26 4
gpt4 key购买 nike

我必须创建一个异构的 RecyclerView。默认列数为 2,但某些项目需要全宽。我在像这样使用 StaggeredGridLayoutManager 时拥有它:

    @Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
int viewType = getItemViewType(position);
FeedItems currentItem = getItem(position);
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
switch (viewType) {
case ITEM_TYPE_HEADER:
final HeaderItem headerItem = currentItem.getHeaderItem();
layoutParams.setFullSpan(true);
}
}

我如何为 GridLayoutManager 做同样的事情,我可以在其中根据不同的 View 类型轻松切换跨度?

最佳答案

有一个方法void setSpanSizeLookup (GridLayoutManager.SpanSizeLookup spanSizeLookup) .像这样使用它:

mLayoutManager = new GridLayoutManager(getActivity(), 2);
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(mAdapter.getItemViewType(position)){
case 1:
return 1;
case 2:
return 2;
default:
return -1;
}
}
});

mRecyclerView.setLayoutManager(mLayoutManager);

关于android - 为 GridLayoutManager 设置完整跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40283311/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com