Android 使用GridLayoutManager的recyclerview中的不同视图类型
我知道以前有很多人问过这个问题,事实上我也曾开发过一个聊天应用程序,它使用Android 使用GridLayoutManager的recyclerview中的不同视图类型,android,android-recyclerview,gridlayoutmanager,Android,Android Recyclerview,Gridlayoutmanager,我知道以前有很多人问过这个问题,事实上我也曾开发过一个聊天应用程序,它使用recyclerview显示聊天,我使用查看类型根据发件人选择消息布局,但我觉得这个问题有所不同 我有一个Recyclerview,它用GridLayoutManager ,在我的图像中,我有3个项目;A、 B和C,我希望C适合宽度,我真的不确定这是否可以通过视图类型实现,下面是我的适配器代码 适配器: public class StoreDisplayItemAdapter extends RecyclerView.Ad
recyclerview
显示聊天,我使用查看类型
根据发件人选择消息布局,但我觉得这个问题有所不同
我有一个Recyclerview
,它用GridLayoutManager
,在我的图像中,我有3个项目;A、 B和C,我希望C适合宽度,我真的不确定这是否可以通过视图类型实现,下面是我的适配器代码
适配器:
public class StoreDisplayItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private ArrayList<StoreDisplayProduct> products;
private Interfaces.OnItemClickedListener onItemClickedListener;
public StoreDisplayItemAdapter(Context context, ArrayList<StoreDisplayProduct> products, Interfaces.OnItemClickedListener onItemClickedListener) {
this.context = context;
this.products = products;
this.onItemClickedListener = onItemClickedListener;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
return new ItemViewHolder(inflater.inflate(R.layout.store_display_item, parent, false));
}
public StoreDisplayProduct getProductsItem(int pos) {
return products.get(pos);
}
public void setProducts(ArrayList<StoreDisplayProduct> products) {
this.products = products;
notifyDataSetChanged();
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
StoreDisplayProduct product = getProductsItem(position);
ItemViewHolder viewHolder = (ItemViewHolder) holder;
viewHolder.txtProductType.setText(product.getProductTypeDisplayName());
viewHolder.txtName.setText(product.getName());
viewHolder.img.post(() -> Picasso.with(context)
.load(product.getDisplayImage().getFullSize())
.fit()
.transform(new RoundedCornersTransformation(10,10))
.placeholder(R.color.asbestos)
.centerCrop()
.into(viewHolder.img));
}
@Override
public int getItemCount() {
return this.products.size();
}
public class ItemViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.txtProductType)
TextView txtProductType;
@BindView(R.id.img)
SquareImageView img;
@BindView(R.id.txtName)
TextView txtName;
public ItemViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
itemView.setOnClickListener(v -> {
onItemClickedListener.onItemClicked(v, getAdapterPosition());
});
}
}
}
您可以根据您的问题@Aubry使用交错网格布局管理器
//you need to add the code for creating a staggered layout manager
recyclerStoreDisplayProducts.setAdapter(storeDisplayItemAdapter);
StaggeredGridLayoutManager staggeredGridLayoutManager = new
StaggeredGridLayoutManager(mNoOfColumns,StaggeredGridLayoutManager.HORIZONTAL);
recyclerStoreDisplayProducts.setHasFixedSize(true);
recyclerStoreDisplayProducts.setNestedScrollingEnabled(true);
recyclerStoreDisplayProducts.setLayoutManager(staggeredGridLayoutManager);
您可以像这样使用跨度计数
GridLayoutManager layoutManager = new GridLayoutManager(this, 4);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (position<3)
return 1;
else
return 2;
}
});
GridLayoutManager-layoutManager=newgridlayoutmanager(这个,4);
layoutManager.setPansizeLookup(新的GridLayoutManager.SpanSizeLookup(){
@凌驾
公共整数getSpanSize(整数位置){
如果(position)查找StaggedGridLayoutManager
。或者您可以尝试。太棒了,该链接解决了我的问题,请将其放在回答部分,以便我批准。
GridLayoutManager layoutManager = new GridLayoutManager(this, 4);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (position<3)
return 1;
else
return 2;
}
});