我有一个包含自定义元素的卡片视图的回收者视图。一切正常,但是当我滚动卡片列表时,卡片视图会改变位置。我该如何控制它。
这是我的适配器类。
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {
ArrayList<Product> Products = new ArrayList<Product>();
public ProductAdapter(ArrayList<Product> Products)
{
this.Products = Products;
}
@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_layout,parent,false);
ProductViewHolder ProductViewHolder = new ProductViewHolder(view);
return ProductViewHolder;
}
@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
Product product = Products.get(position);
holder.pic.setImageBitmap(product.getBitmap());
holder.name.setText(product.getName());
holder.size.setText(product.getSize());
holder.price.setText(product.getPrice());
}
@Override
public int getItemCount() {
return Products.size();
}
public static class ProductViewHolder extends RecyclerView.ViewHolder {
private ImageView pic;
private TextView name,size,price;
public ProductViewHolder(View view)
{
super(view);
pic = (ImageView) view.findViewById(R.id.pic);
name = (TextView) view.findViewById(R.id.name);
size = (TextView) view.findViewById(R.id.size);
price = (TextView) view.findViewById(R.id.price);
}
}
}
答案 0 :(得分:0)
将LinearLayout的layout_height更改为“wrap_content”。 它会起作用。
答案 1 :(得分:-3)
我认为你需要覆盖这些方法......
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}