MultiChoiceModeListener Android

时间:2015-09-30 12:21:29

标签: android

我想从gridView中删除一些图像,一切都可以正常使用MultiChoiceModeListener,但是当我选择要删除的图片时,我想在边距上创建蓝色图像,并在图像上添加一些蓝色图像。 (就像在Android画廊中,在我点击随机照片之后)。谢谢!

  public class MultiChoiceModeListener implements
        GridView.MultiChoiceModeListener {


    @Override
    public void onItemCheckedStateChanged(android.view.ActionMode mode, int position, long id, boolean checked) {



        int selectCount = grid.getCheckedItemCount();


        SparseBooleanArray checkedItemPositions = grid.getCheckedItemPositions();

        int itemCount = grid.getCount();


        switch (selectCount) {
            case 1:
                mode.setSubtitle("One picture selected");

                break;
            default:
                mode.setSubtitle("" + selectCount + " pictures selected");
                break;
        }
    }

    @Override
    public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
        mode.setTitle("Select pictures");
        mode.setSubtitle("One picture selected");



        return true;

    }

    @Override
    public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
        return true;
    }

    @Override
    public boolean onActionItemClicked(android.view.ActionMode mode,    MenuItem item) {
        return true;
    }

    @Override
    public void onDestroyActionMode(android.view.ActionMode mode) {

    }
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_add:


            // choose picture from gallery
            Intent PhotoPickerIntent = new Intent(Intent.ACTION_PICK,    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            File pictureDirectory =   Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            String pictureDirectoryPath = pictureDirectory.getPath();
            Uri data = Uri.parse(pictureDirectoryPath);
            PhotoPickerIntent.setDataAndType(data, "image/* video*/");


            startActivityForResult(PhotoPickerIntent,
                    REQUEST_CODE_EXTERNAL_IMAGE);

            break;
        case R.id.action_delete:


            File filepath = Environment.getExternalStorageDirectory();

            SparseBooleanArray checkedItemPositions =    grid.getCheckedItemPositions();

            int itemCount = grid.getCount();


            for (int i = itemCount - 1; i >= 0; i--) {
                if (checkedItemPositions.get(i)) {

                    File dir5 = new File(filepath.getAbsolutePath()
                            + nameAlbum + FileNameStrings[i]);

                    File file3 = new File(String.valueOf(dir5));
                    deleted = file3.delete();




                }


                }
            if(deleted==true) {

                checkedItemPositions.clear();
  finish();
                Intent INTENT = new Intent(this, AlbumActivity.class);
                INTENT.putExtra("nameAlbum",nameAlbum);
                INTENT.putExtra("nameAlbum2",nameAlbum2);
                startActivity(INTENT);


            }else {
                AlertDialog.Builder builder = new   AlertDialog.Builder(AlbumActivity.this);
                builder.setMessage("Keep hold on a picture until you can  select pictures for delete!");
                builder.setTitle("ERROR!");
                builder.setCancelable(false);
                builder.setNeutralButton("OK", new   DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
            }




    break;


            }
            return super.onMenuItemSelected(featureId, item);

    }

编辑:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:state_activated="true"
    android:drawable="@color/md_blue_400"/>
<item android:state_pressed="true"
    android:drawable="@color/md_black_1000_50" />


</selector>

这个选择器你将像gridview中的imageview一样使用背景!而且我认为这会为你解决问题!

1 个答案:

答案 0 :(得分:1)

我用选择器解决了我的问题,如果你在gridview上使用多重侦听器,你必须在drawble文件夹中创建一个选择器,并将它像gridview_item的bacground文件一样!我希望这有助于某人