如何在多图像视图中处理长按

时间:2019-03-25 04:49:49

标签: android onlongclicklistener

我的布局中有8个数字ImageView,并且我已经将LongClickListner设置为所有ImageView。如果用户长按图像(任何图像),则图像将保存在图库中。但是我的问题是,当用户长按第8张图片时,第1张图片或其他图片(其中1张)会保存在图库中。我想要的是,当用户长按第8张图片时,第8张图片将不会保存其他图片。

 @Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.saveImage:
            if(img1.isLongClickable() ){
                BitmapDrawable bitmapDrawable = (BitmapDrawable)img1.getDrawable();
                Bitmap bitmap = bitmapDrawable.getBitmap();
                Random random = new Random();
                int x = random.nextInt(1000000);
                saveImageUtils.insertImage(getActivity().getContentResolver(),bitmap,String.valueOf(x));
                //SaveImage(bitmap);
                Toast.makeText(getContext(), "Image Saved to Device", Toast.LENGTH_SHORT).show();
                break;
            }else if(img2.isLongClickable()){
                BitmapDrawable bitmapDrawable = (BitmapDrawable)img2.getDrawable();
                Bitmap bitmap = bitmapDrawable.getBitmap();
                Random random = new Random();
                int x = random.nextInt(1000000);
                saveImageUtils.insertImage(getActivity().getContentResolver(),bitmap,String.valueOf(x));
                //SaveImage(bitmap);
                Toast.makeText(getContext(), "Image Saved to Device", Toast.LENGTH_SHORT).show();
                break;
            }else if(img3.isLongClickable()){
                BitmapDrawable bitmapDrawable = (BitmapDrawable)img3.getDrawable();
                Bitmap bitmap = bitmapDrawable.getBitmap();
                Random random = new Random();
                int x = random.nextInt(1000000);
                saveImageUtils.insertImage(getActivity().getContentResolver(),bitmap,String.valueOf(x));
                //SaveImage(bitmap);
                Toast.makeText(getContext(), "Image Saved to Device", Toast.LENGTH_SHORT).show();
                break;
            }else if(img4.isLongClickable()){
                BitmapDrawable bitmapDrawable = (BitmapDrawable)img4.getDrawable();
                Bitmap bitmap = bitmapDrawable.getBitmap();
                Random random = new Random();
                int x = random.nextInt(1000000);
                saveImageUtils.insertImage(getActivity().getContentResolver(),bitmap,String.valueOf(x));
                //SaveImage(bitmap);
                Toast.makeText(getContext(), "Image Saved to Device", Toast.LENGTH_SHORT).show();
                break;
            }else if(img5.isLongClickable()){
                BitmapDrawable bitmapDrawable = (BitmapDrawable)img5.getDrawable();
                Bitmap bitmap = bitmapDrawable.getBitmap();
                Random random = new Random();
                int x = random.nextInt(1000000);
                saveImageUtils.insertImage(getActivity().getContentResolver(),bitmap,String.valueOf(x));
                //SaveImage(bitmap);
                Toast.makeText(getContext(), "Image Saved to Device", Toast.LENGTH_SHORT).show();
                break;
            }else if(img6.isLongClickable()){
                BitmapDrawable bitmapDrawable = (BitmapDrawable)img6.getDrawable();
                Bitmap bitmap = bitmapDrawable.getBitmap();
                Random random = new Random();
                int x = random.nextInt(1000000);
                saveImageUtils.insertImage(getActivity().getContentResolver(),bitmap,String.valueOf(x));
                //SaveImage(bitmap);
                Toast.makeText(getContext(), "Image Saved to Device", Toast.LENGTH_SHORT).show();
                break;
            }else if(img7.isLongClickable()){
                BitmapDrawable bitmapDrawable = (BitmapDrawable)img7.getDrawable();
                Bitmap bitmap = bitmapDrawable.getBitmap();
                Random random = new Random();
                int x = random.nextInt(1000000);
                saveImageUtils.insertImage(getActivity().getContentResolver(),bitmap,String.valueOf(x));
                //SaveImage(bitmap);
                Toast.makeText(getContext(), "Image Saved to Device", Toast.LENGTH_SHORT).show();
                break;
            }else if(img8.isLongClickable()){
                BitmapDrawable bitmapDrawable = (BitmapDrawable)img8.getDrawable();
                Bitmap bitmap = bitmapDrawable.getBitmap();
                Random random = new Random();
                int x = random.nextInt(1000000);
                saveImageUtils.insertImage(getActivity().getContentResolver(),bitmap,String.valueOf(x));
                //SaveImage(bitmap);
                Toast.makeText(getContext(), "Image Saved to Device", Toast.LENGTH_SHORT).show();
                break;
            }

    }
    return super.onContextItemSelected(item);
}

1 个答案:

答案 0 :(得分:1)

您上面呈现的内容是这样的:

  1. 您的ImageView显然已分配了OnLongClickListener
  2. 用户单击工具栏的菜单项R.id.saveImage,而您正在尝试根据isLongClickable的指示保存图像

如果为true,则上述错误是因为isLongClickable基本上仅表示给定控件已分配了OnLongClickListener

您应该做什么:在每个OnLongClickListener中进行保存(并且其onLongClick将为您提供视图,即ImageView),或者仅在以下情况下进行保存: R.id.saveImage单击,然后在OnLongClickListener中以某种方式告诉您的应用,给定的ImageView被长按,然后R.id.saveImage单击,遍历ImageView长单击的记录列表并执行保存。