androidx RecycleView 图像重复问题

如图,在学习RecycleView时试着实现书上的代码,遇到问题,为何每个图像都会有两个图片,我分明在onBindViewHolder重新设置了图片的imageResource

public class FruitAdapter2 extends RecyclerView.Adapter<FruitAdapter2.ViewHolder> {
    private List<Fruit> mFruitList;

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView fruitImage;
        TextView fruitName;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            fruitImage = (ImageView) itemView.findViewById(R.id.iv_fruit);
            fruitName = (TextView) itemView.findViewById(R.id.tv_fruit);
        }
    }

    public FruitAdapter2(List<Fruit> fruitList) {
        mFruitList = fruitList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, null, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Fruit fruit = mFruitList.get(position);
        holder.fruitImage.setImageResource(fruit.getFruitImage());
        holder.fruitName.setText(fruit.getFruitName());
    }

    @Override
    public int getItemCount() {
        return mFruitList.size();
    }

}

 

查看全部
weixin_43323172
Jackie·Tang
2020/11/24 12:30
  • android-studio
  • android
  • 点赞
  • 收藏
  • 已关闭
    私信
满意答案
查看全部

2个回复