RecyclerView添加项目,删除项目什么是最佳解决方案?

时间:2018-01-28 16:17:29

标签: android android-recyclerview picasso

我到了一个地步,我感到非常沮丧。

RecyclerView是aweseom!我喜欢在我的应用程序中使用它,但是我通过删除和添加项目,在删除或更新时更新列表来面对问题。

所以我确实有一个recyclerview,其中图像加载了Picasso。我从我的数据库中获取了URL。

添加图像是有效的,问题是,当上传图片时,notifyDataSetChanged没有改变视图,即使我把它的位置改变了。我不想重新加载活动。

我在活动中尝试了以下内容:

adapter.notifyDataSetChanged();

什么都没发生。

recyclerView.getAdapter().notifyDataSetChanged();

什么都没发生。

profilAdapterImages.notifyItemInserted(profilAdapterImages.getItemCount()+1);

什么都没发生

我想在 adpater中尝试

 public void update(List<getterImages> list){
    this.list.clear();
    this.list = list;
    notifyDataSetChanged();
}

什么都没有显示。

当我删除图像时,它会在数据库中删除。 在数据库中,URL的存储方式如下:

IMAGELINK

http://www.mysite.de/images/user/image123.png;http://www.mysite.de/images/user/image1234.png;http://www.mysite.de/images/user/image12345.png

E.g我删除了数据库中的第二个图像:

IMAGELINK

http://www.mysite.de/images/user/image123.png;http://www.mysite.de/images/user/image12345.png

现在我的问题是,第一张图片正在显示但是在第一张图像之后 它没有加载。在我看来,我必须使用列表或适配器 但我不知道。

首先,我从列表中删除了网址。     list.remove(位置);

然后我从我的数据库中删除数据,从我的网站空间删除图像。

之后,只显示第一张图片,其他图片未加载。

这是图片将被删除的地方:

   profilAdapterImages = new ProfilAdapterImages(list, Profil.this, new ImageSettingsCallback() {
                    @Override
                    public void onImageSettings(final int position) {
                        final Dialog imageSettings = new Dialog(Profil.this);
                        imageSettings.setContentView(R.layout.edit_image);

                        Button buttonDeleteImage = imageSettings.findViewById(R.id.buttonDeleteImage);
                        Button buttonAddImage = imageSettings.findViewById(R.id.buttonAddImage);

                        buttonDeleteImage.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                updateData updateData = new updateData(Profil.this);

                                String[] filename = list.get(position).getImageURL().split("/");


                                Map<String,String> deleteFromServer = new HashMap<>();
                                deleteFromServer.put("mode","5");
                                deleteFromServer.put("username",USERNAME);
                                deleteFromServer.put("filename",filename[6]);
                                updateData.sendDataUpdate(deleteFromServer, new VolleyCallback() {
                                    @Override
                                    public void onUserExists(String[] result) {
                                    }

                                    @Override
                                    public void OnSuccess() {

                                    }

                                    @Override
                                    public void OnError(String ErrorMessage) {

                                    }
                                });

                                list.remove(position);

                                List<String> strings = new ArrayList<>();
                                for(int i = 0; i < list.size();i++){
                                    strings.add(list.get(i).getImageURL());
                                }
                                CharSequence[] cs = strings.toArray(new CharSequence[strings.size()]);

                                // Arrays.toString(cs).replace(",",";").replace("[","").replace("]","")



                                Map<String,String> map = new HashMap<>();
                                map.put("username",USERNAME);
                                map.put("mode","4");
                                map.put("row","IMAGELINK");
                                map.put("data",Arrays.toString(cs).replace(",",";").replace("[","").replace("]",""));
                                updateData.sendDataUpdate(map, new VolleyCallback() {
                                    @Override
                                    public void onUserExists(String[] result) {

                                    }

                                    @Override
                                    public void OnSuccess() {

                                    }

                                    @Override
                                    public void OnError(String ErrorMessage) {

                                    }
                                });

                                imageSettings.dismiss();
                            }
                        });

这是我的适配器:

public class ProfilAdapterImages extends RecyclerView.Adapter<ProfilAdapterImages.ViewHolder>{

    List<getterImages> list;
    Context context;
    ImageSettingsCallback imageSettingsCallback;

    public ProfilAdapterImages(List<getterImages> list, Context context, ImageSettingsCallback imageSettingsCallback) {
        this.list = list;
        this.context = context;
        this.imageSettingsCallback = imageSettingsCallback;
    }

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

    @Override
    public void onBindViewHolder(final ProfilAdapterImages.ViewHolder holder, final int position) {
        getterImages getterImages = list.get(position);

        Picasso.with(context).load(getterImages.getImageURL()).resize(200,200).centerCrop().into(holder.blurImageView, new Callback() {
            @Override
            public void onSuccess() {
                holder.blurImageView.setBlur(20);
            }

            @Override
            public void onError() {

            }
        });

        Picasso.with(context).load(getterImages.getImageURL()).placeholder(R.drawable.preload).resize(200,200).centerCrop().into(holder.imageView, new Callback() {
            @Override
            public void onSuccess() {
                RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(),drawableToBitmap(holder.imageView.getDrawable()));
                roundedBitmapDrawable.setCircular(true);
                holder.imageView.setImageDrawable(roundedBitmapDrawable);
            }

            @Override
            public void onError() {

            }
        });

        holder.imageButtonImageSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imageSettingsCallback.onImageSettings(position);
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        BlurImageView blurImageView;
        ImageView imageView;
        ImageButton imageButtonImageSettings;


        public ViewHolder(View itemView) {
            super(itemView);
            blurImageView = itemView.findViewById(R.id.imageViewSlider);
            imageView = itemView.findViewById(R.id.imageView);
            imageButtonImageSettings = itemView.findViewById(R.id.imageButtonImageSettings);
        }
    }

    public void update(List<getterImages> list){
        this.list.clear();
        this.list = list;
        notifyDataSetChanged();
    }

    public static Bitmap drawableToBitmap (Drawable drawable) {
        Bitmap bitmap = null;

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if(bitmapDrawable.getBitmap() != null) {
                return bitmapDrawable.getBitmap();
            }
        }

        if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    }
}

所以小伙子们,如果你能解决我的问题,我将非常感激。

感谢德国的问候。

0 个答案:

没有答案