RecyclerView正在复制列表中的前三个项目

时间:2019-04-01 23:26:31

标签: java android android-recyclerview

我对android来说还很陌生,我正在尝试使用RecyclerView来显示托管在Firebase上的内容,但是当它出现时,前三个项目就会重复。

我已经尝试了一些解决方案,但是似乎都没有用,任何帮助都会很棒!

DiscountRecyclerAdapter.java

public class DiscountRecyclerAdapter extends RecyclerView.Adapter<com.cianod.comharapp.DiscountRecyclerAdapter.ViewHolder> {

        public List<Discounts> discountsList;
        public Context context;
        private ImageView discountImageView;

        public DiscountRecyclerAdapter(List<Discounts> discountsList){

            this.discountsList = discountsList;

        }

        @Override
        public com.cianod.comharapp.DiscountRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.discount_list_item, parent, false);
            context = parent.getContext();
            return new com.cianod.comharapp.DiscountRecyclerAdapter.ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final com.cianod.comharapp.DiscountRecyclerAdapter.ViewHolder holder, int position) {

            holder.setIsRecyclable(false);

            String discountName = discountsList.get(position).getDiscount_name();
            holder.setDiscount_Name(discountName);

            String discountDescription = discountsList.get(position).getDiscount_description();
            holder.setDiscount_Description(discountDescription);

            String discountValue = discountsList.get(position).getDiscount_value();
            holder.setDiscount_Value(discountValue);


            String image_url = discountsList.get(position).getDiscount_image();
            String thumbUri = discountsList.get(position).getDiscount_image();
            holder.setDiscount_Image(image_url, thumbUri);

        }


        @Override
        public int getItemCount() {

            if(discountsList != null) {

                return discountsList.size();

            } else {

                return 0;

            }

        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            private View mView;

            private TextView discount_name;

            public ViewHolder(View itemView) {
                super(itemView);
                mView = itemView;
            }

            public void setDiscount_Name(String message){

                discount_name = mView.findViewById(R.id.discount_name);
                discount_name.setText(message);

            }

            public void setDiscount_Description(String message){

                discount_name = mView.findViewById(R.id.discount_description);
                discount_name.setText(message);

            }

            public void setDiscount_Value(String message){

                discount_name = mView.findViewById(R.id.discount_value);
                discount_name.setText(message);

            }

            public void setDiscount_Image(String downloadUri, String thumbUri){

                discountImageView = mView.findViewById(R.id.discount_image);

                RequestOptions requestOptions = new RequestOptions();
                requestOptions.placeholder(R.drawable.image_placeholder);

                Glide.with(context).applyDefaultRequestOptions(requestOptions).load(downloadUri).thumbnail(
                        Glide.with(context).load(thumbUri)
                ).into(discountImageView);

            }

        }

    }

我相信问题来自折扣适配器,但我不能肯定地说。如果碎片可能会影响RecyclerView的显示,可以在需要时附加其他碎片。

1 个答案:

答案 0 :(得分:0)

您的代码没有错。因此,也许您应该尝试在适配器类中覆盖这两个方法

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
   return position;
}