我知道以前有很多人问过这个问题,实际上我也曾经在使用recyclerview
来显示聊天的聊天应用程序上工作,我使用view types
根据发件人,但我觉得这个问题不一样。
我有一个Recyclerview
,正在显示带有GridLayoutManager
的内容
heres an image to show that,在我的图片中,我有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());
});
}
}
}
以及我如何使用适配器
recyclerStoreDisplayProducts.setAdapter(storeDisplayItemAdapter);
recyclerStoreDisplayProducts.setLayoutManager(new GridLayoutManager(getContext(), 2));
recyclerStoreDisplayProducts.setHasFixedSize(true);
recyclerStoreDisplayProducts.setNestedScrollingEnabled(true);
答案 0 :(得分:0)
您可以根据自己的问题@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);
答案 1 :(得分:0)
您可以使用跨度计数
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;
}
});