RecyclerView添加项目

时间:2018-01-29 21:55:54

标签: android android-recyclerview

通过保存新图像,我想将其添加到RecyclerView。 我的问题是它无法解决这个问题:

 profilAdapterImages.notifyItemInserted(list.size() -1);
 profilAdapterImages.notifyDataSetChanged();

我不想重新加载活动,我想要动画。

当我将图像保存在数据库中时,这是在onActivityResult中。

savePicture.setOnClickListener(new View.OnClickListener() {
   @Override
    public void onClick(View view) {
    progressBar.setVisibility(View.VISIBLE);
    encodeBase64 base64 = new encodeBase64();

    Map<String,String> map = new HashMap<>();
    map.put("username",USERNAME);
    map.put("row","IMAGELINK");
    map.put("data",base64.encodeImage(bitmap, 
    Bitmap.CompressFormat.JPEG,70));
    map.put("mode","2");
    updateData updateData = new updateData(Profil.this);
    updateData.sendDataUpdate(map, new VolleyCallback() {
        @Override
        public void onUserExists(String[] result) {
          if(result[0].equals("ok")){
              previewPicture.dismiss();
              Toast.makeText(Profil.this, "Dein Bild wurde gespeichert!", Toast.LENGTH_SHORT).show();

                profilAdapterImages.notifyItemInserted(list.size() -1);
                profilAdapterImages.notifyDataSetChanged();
        }
    }

    @Override
    public void OnSuccess() {

    }

    @Override
    public void OnError(String ErrorMessage) {

    }
});

progressBar.setVisibility(View.VISIBLE);
}

适配器:

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 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;
}
}

我错了什么?

我从我的网站空间加载Picasso的图片。 在数据库中,它们被保存在一行中,并用分号分隔。

2 个答案:

答案 0 :(得分:0)

除非适配器 RecyclerView 自行刷新,否则

通知profilAdapterImages.notifyDataSetChanged();不会执行任何操作,这意味着您必须滚动才能进行更改

或者您有其他选择:

我会自己更换适配器。

例如,如果您最初设置适配器:

ProfilAdapterImages  mAdapter = new ProfilAdapterImages(list, context, imageSettingsCallback);
recyclerview.setAdapter(mAdapter);
recyclerview.setHasFixedSize(true);
mAdapter.notifyDataSetChanged();

你有你的适配器,只需用它自己交换适配器:

savePicture.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      ProfilAdapterImages  nAdapter  = new ProfilAdapterImages(list, context, imageSettingsCallback);
      recyclerview.setAdapter(nAdapter);
      recyclerview.setHasFixedSize(true);
      nAdapter.notifyDataSetChanged();

      ....
    }

答案 1 :(得分:0)

OP解决方案。

我加载了数据new,清除了列表并添加了 它是new和notifyDataSetChanged和smoothScrollToPosition。