从arraylist图像删除图像时删除但是在添加新图像到arraylist之前加载的图像

时间:2018-01-25 11:00:00

标签: android image-processing arraylist android-recyclerview

这是设置图像回收器视图的类,并且从数组列表中的文件夹添加图像并将其加载到图像视图。我的问题是当图像被删除时它会被删除但是在添加新图像之后图像被加载在图片视图中,请查看我的代码,并为我提供适当的解决方案..

private ImageAdapterone imageAdapterone;
File file;

MenuItem item;
ArrayList<String> f;// list of file paths
File[] listFile;
RecyclerView recyclerImage;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid_layout);

    f = new ArrayList<String>();
    getFromSdcard();

    Runtime rt = Runtime.getRuntime();
    long maxMemory = rt.maxMemory();

    System.out.println("memory is : " + maxMemory);


    recyclerImage = (RecyclerView) findViewById(R.id.recyclerImage);


    imageAdapterone = new ImageAdapterone();
    int numberOfColumns = 2;

    GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
    // GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),2,LinearLayoutManager.VERTICAL,false);
    //gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerImage.setLayoutManager(gridLayoutManager);

    recyclerImage.setAdapter(imageAdapterone);


    recyclerImage.addOnItemTouchListener(new
            RecyclerItemClickListener(CustomActivity.this, recyclerImage, new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {

            item.setVisible(false);
            // correctmark.setVisibility(View.INVISIBLE);

            if (position == 0) {
                gotoNext(position, "rougescript_regular", R.color.colorWhite);
            }
            if (position == 1) {
                gotoNext(position, "tangerine_regular", R.color.colorWhite);
            }
            if (position == 2) {
                gotoNext(position, "gabriola", R.color.colorWhite);
            }
            if (position == 3) {
                gotoNext(position, "tangerine_regular", R.color.colorBlack);
            }
            if (position == 4) {
                gotoNext(position, "tangerine_regular", R.color.colorWhite);
            }
            if (position == 5) {
                gotoNext(position, "tangerine_regular", R.color.colorWhite);
            }
            if (position == 6) {
                gotoNext(position, "DancingScript-Regular", R.color.colorBlack);
            }
            if (position == 7) {
                gotoNext(position, "jimnightshade_regular", R.color.colorWhite);
            }
            if (position == 8) {
                gotoNext(position, "gabriola", R.color.colorPrimaryDark);
            }
            if (position == 9) {
                gotoNext(position, "rougescript_regular", R.color.colorBlack);
            }
            if (position == 10) {
                gotoNext(position, "rougescript_regular", R.color.colorBlack);
            }
            if (position == 11) {
                gotoNext(position, "jimnightshade_regular", R.color.colorWhite);
            }
            if (position == 12) {

                Intent intent = new Intent(CustomActivity.this, EditSettingActivity.class);
                startActivity(intent);
            }
            for (int i = 13; i < f.size(); i++) {
                goToNextActivity(position, "jimnightshade_regular", i, R.color.colorWhite);
            }

            SharedPreferences.Editor editor = MyApplication.getPref().edit();
            editor.putInt("ImagePosition", position);
            editor.apply();

        }

        @Override
        public void onLongItemClick(View view, int position) {
            //Toast.makeText(CustomActivity.this, position, Toast.LENGTH_SHORT).show();
            System.out.println("delete file position is on: " + position);


            for (int i = 13; i < f.size(); i++) {
                // for (int i = 13; i >= 0; i--) {
                if (position == i) {
                    item.setVisible(true);


                    SharedPreferences.Editor editor = MyApplication.getPref().edit();
                    editor.putInt("DeletePosition", i);
                    editor.apply();


                    System.out.println("delete file position is on: " + MyApplication.getPref().getInt("DeletePosition", 0));

                }
            }
        }
    }));


}

private void goToNextActivity(int position, String font, int i, int color) {
    if (position == i) {
        Intent intent = new Intent(CustomActivity.this, FullImageActivity.class);
        System.out.println("array position is : " + f.get(i));

        intent.putExtra("idone", f.get(i));
        intent.putExtra("font", font);
        intent.putExtra("CurrentFlag", "1");


        SharedPreferences.Editor editor = MyApplication.getPref().edit();
        editor.putString("ArrayPosition", f.get(i));
        editor.putInt("Color", color);
        editor.apply();
        startActivity(intent);

    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main2, menu);
    item = menu.findItem(R.id.action_delete);
    item.setOnMenuItemClickListener(this);
    return super.onCreateOptionsMenu(menu);
}


public void getFromSdcard() {

    file = new File(android.os.Environment.getExternalStorageDirectory(), "Wallpaper Pack");
    System.out.println("folder is sdcard : " + file);

    if (file.isDirectory()) {
        listFile = file.listFiles();
        System.out.println("list of file is: " + listFile.length);

        for (int i = 0; i < listFile.length; i++) {

            f.add((listFile[i].getAbsolutePath()));


            System.out.println("path is : " + listFile[i].getAbsolutePath());

        }
    }
}

public void gotoNext(int id, String font, int color) {


    Intent intent = new Intent(CustomActivity.this, FullImageActivity.class);
    intent.putExtra("id", id);
    intent.putExtra("font", font);
    intent.putExtra("fonttag", "1");
    intent.putExtra("CurrentFlag", "0");
    intent.putExtra("color", color);


    SharedPreferences.Editor editor = MyApplication.getPref().edit();
    editor.putString("NewFont", font);
    editor.putInt("Color", color);
    editor.apply();

    startActivity(intent);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
}


@Override
protected void onDestroy() {
    super.onDestroy();
    Glide.get(this).clearMemory();
}


@Override
public boolean onMenuItemClick(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.action_delete: 

   File(listFile[MyApplication.getPref().getInt("DeletePosition",0)].
   getAbsolutePath());
  +listFile[MyApplication.getPref().getInt("DeletePosition",0)]
  .getAbsolutePath());

   imageAdapterone.notifyItemRemoved(MyApplication.getPref()
  .getInt("DeletePosition",0));

            File file = new 
   File(listFile[MyApplication.getPref().getInt("DeletePosition", 
   0)].getAbsolutePath());


            file.delete();
            imageAdapterone.deleteItem(MyApplication.getPref()
         .getInt("DeletePosition", 0));
            imageAdapterone.notifyDataSetChanged();
            item.setVisible(false);


            break;

    }
    return false;
}

现在这是适配器的代码,其中使用滑动设置图像arraylist ..

         public class ImageAdapterone extends RecyclerView.Adapter<ImageAdapterone.SingleTestRowHolder> {

    Context context;
    Bitmap myBitmap;

    @Override
    public SingleTestRowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.galleryitem, null);
        SingleTestRowHolder mh = new SingleTestRowHolder(v);
        return mh;
    }


    @Override
    public void onBindViewHolder(ImageAdapterone.SingleTestRowHolder holder, int position) {

        byte[] encodeByte = Base64.decode(f.get(position), Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);

        if (position > 12) {
            holder.customText.setVisibility(View.VISIBLE);
        } else {
            holder.customText.setVisibility(View.INVISIBLE);
        }

        System.out.println("IMAGE POSITION: " + f.get(position));

        Glide.with(holder.thumbImage.getContext())
                .load(f.get(position))
                .override(300, 300)
                .centerCrop()

        StringSignature(String.valueOf(System.currentTimeMillis())))


                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(holder.thumbImage);
    }

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

    public void deleteItem(int index) {
        System.out.println("INDEX OF DELETED ITEM: " + index);
        f.remove(index);

        notifyItemRemoved(index);
        notifyItemRangeChanged(index, f.size());

        System.out.println("IMAGES SIZE: " + f.size());

    }


    public class SingleTestRowHolder extends RecyclerView.ViewHolder {
        ImageView thumbImage;
        TextView customText;

        public SingleTestRowHolder(View itemView) {
            super(itemView);
            thumbImage = (ImageView) itemView.findViewById(R.id.thumbImage);
            customText = (TextView) itemView.findViewById(R.id.customText);

        }
    }
}

0 个答案:

没有答案