如何一次删除多个gridview项目

时间:2018-01-03 10:14:10

标签: java android gridview delete-file image-gallery

我创建了一个图片库应用,将图像显示为 gridview 。我可以一次选择多张照片,但我无法通过单击删除按钮一次删除所有照片。 我没有使用复选框,因为它不适合我的应用程序的设计。我试图找到一种方法,但几乎所有人都使用复选框。任何人都建议我如何删除多张照片一次不使用复选框?

PhotosActivity.java

public class PhotosActivity extends AppCompatActivity {
    int int_position;
    private GridView gridView;
    GridViewAdapter adapter;
    ArrayList<Model_images> al_menu = new ArrayList<>();
    private ArrayList<Object> mSelected = new ArrayList<>();
    boolean boolean_folder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_gallery);

        final ImageButton button1 = (ImageButton) findViewById(R.id.button1);
        final ImageButton button2 = (ImageButton) findViewById(R.id.button2);
        final ImageButton button3 = (ImageButton) findViewById(R.id.button3);
        final ImageButton button4 = (ImageButton) findViewById(R.id.button4);
        final ImageButton button5 = (ImageButton) findViewById(R.id.button5);
        button1.setVisibility(View.GONE);
        button2.setVisibility(View.GONE);
        button3.setVisibility(View.GONE);
        button4.setVisibility(View.GONE);
        button5.setVisibility(View.GONE);

        gridView = (GridView) findViewById(android.R.id.list);
        int_position = getIntent().getIntExtra("value", 0);
        adapter = new GridViewAdapter(this, al_images, int_position);
        gridView.setAdapter(adapter);
        gridView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String abc = "file://" + al_images.get(int_position).getAl_imagepath().get(position);

                Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                i.putExtra("id", position);
                i.putExtra("folderPosition", int_position);
                i.putExtra("abc", abc);
                startActivity(i);
            }
        });

        gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                String position1 = new String(al_images.get(int_position).getAl_imagepath().get(position));
                if(mSelected.contains(position1)) {
                    mSelected.remove(position1);
                    view.setBackgroundColor(Color.TRANSPARENT);// remove item from list
                    // update view (v) state here
                    // eg: remove highlight
                }
                else {
                    mSelected.add(position1);
                    view.setBackgroundColor(Color.LTGRAY);// add item to list
                    // update view (v) state here
                    // eg: add highlight
                }


                button1.setVisibility(View.VISIBLE);
                button2.setVisibility(View.VISIBLE);
                button3.setVisibility(View.VISIBLE);
                button4.setVisibility(View.VISIBLE);
                button5.setVisibility(View.VISIBLE);
                button3.setOnClickListener(
                        new View.OnClickListener() {
                            public void onClick(View view) {
                                AlertDialog.Builder builder1 = new AlertDialog.Builder(PhotosActivity.this);
                                builder1.setMessage("Are you sure you want to delete it ?");
                                builder1.setCancelable(true);

                                builder1.setPositiveButton(
                                        "Yes",
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int id) {
                                                deleteSelectedItems();
                                                al_images.remove(position);
                                                adapter.notifyDataSetChanged();
                                                finish();
                                            }
                                        });

                                builder1.setNegativeButton(
                                        "No",
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int id) {
                                                dialog.cancel();
                                            }
                                        });

                                AlertDialog alert11 = builder1.create();
                                alert11.show();
                            }
                        });

                return true;
            }
        });
    }
    private void deleteSelectedItems() {
        SparseBooleanArray checked = gridView.getCheckedItemPositions();
        if (checked != null) {
            List<Object> list = mSelected;
            for (int i = 0; i < checked.size(); i++) {
                if (checked.get(i)) {
                    Object item = list.get(i);
                    mSelected.remove(item);
                    checked.delete(i);
                }
            }
        }
    }

GridviewAdapter.java

public class GridViewAdapter extends ArrayAdapter<Model_images> {

    Context context;
    ViewHolder viewHolder;
    ArrayList<Model_images> al_menu = new ArrayList<>();
    int int_position;

    public GridViewAdapter(Context context, ArrayList<Model_images> al_menu,int position) {
        super(context, R.layout.activity_adapter__photos_folder, al_menu);
        this.al_menu = al_menu;
        this.context = context;
        this.int_position = position;
    }

    @Override
    public int getCount() {
        Log.e("ADAPTER LIST SIZE", al_menu.size() + "");
        Log.e("ADAPTER LIST SIZE", al_menu.get(int_position).getAl_imagepath().size() + "");
        return al_menu.get(int_position).getAl_imagepath().size();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        if (al_menu.size() > 0) {
            return al_menu.get(int_position).getAl_imagepath().size();
        } else {
            return 1;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {

            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_adapter__photos_folder, parent, false);
            viewHolder.tv_foldern = (TextView) convertView.findViewById(R.id.tv_folder);
            viewHolder.tv_foldersize = (TextView) convertView.findViewById(R.id.tv_folder2);
            viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tv_foldern.setVisibility(View.GONE);
        viewHolder.tv_foldersize.setVisibility(View.GONE);


        Glide.with(context).load("file://" + al_menu.get(int_position).getAl_imagepath().get(position))
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .into(viewHolder.iv_image);


        return convertView;

    }

    private static class ViewHolder {
        TextView tv_foldern, tv_foldersize;
        ImageView iv_image;

    }
}

3 个答案:

答案 0 :(得分:1)

你可以这样做

在活动中 -

  public class PhotosActivity extends AppCompatActivity {
    ...
    private ArrayList<Integer> mSelected = new ArrayList<>();
    ...
    gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                    if(mSelected.contains(position)) {
                        mSelected.remove(position);
                        view.setBackgroundColor(Color.TRANSPARENT);// remove item from list
                        // update view (v) state here
                        // eg: remove highlight
                    }
                    else {
                        mSelected.add(position);
                        view.setBackgroundColor(Color.LTGRAY);// add item to list
                        // update view (v) state here
                        // eg: add highlight
                  }
                  .....
                  .....
                  .....

  }

现在调用适配器的方法,如删除btn点击 -

adapter.updateUpdapter(mSelecected);

在适配器中 -

 public GridViewAdapter(Context context, ArrayList<Model_images> al_menu,int position) {
        super(context, R.layout.activity_adapter__photos_folder, al_menu);
        this.al_menu.addAll(al_menu);
       ....
    }

private void updateUpdapter(ArrayList<Integer> mSelected){
        for(int selected: mSelected){
            al_menu.get(int_position).getAl_imagepath().remove(selected);
        }
        notifiDataSetChanged();
    }

我希望它会有所帮助。

答案 1 :(得分:0)

我认为这是因为您只从适配器中删除了一个位置。

... 
public void onClick(DialogInterface dialog, int id) {
                                                deleteSelectedItems();
                                                al_images.remove(position);
                                                adapter.notifyDataSetChanged();
...

我目前无法测试代码,但请尝试:

private void deleteSelectedItems() {
        SparseBooleanArray checked = gridView.getCheckedItemPositions();
        if (checked != null) {
            List<Object> list = mSelected;
            for (int i = 0; i < checked.size(); i++) {
                if (checked.valueAt(i)) {
                    mSelected.remove(checked.keyAt(i));
                    al_images.remove(checked.keyAt(i));
                }
            }
        }
    }

答案 2 :(得分:0)

选择并删除多个项目:

  1. 您选择项目的那一刻会在数组或列表中存储该项目的位置或唯一值。

  2. 选择项目后,当用户按下Delete按钮循环浏览主要项目并从中删除所选项目时。

  3. 通过调用notifyDataSetChanged();

    刷新适配器

    //based on item position
    List < Integer > selectedItems = new ArrayList < > ();
    public void onSelected(int pos) { selectedItems.add(pos); }
    public deleteItems() { for (Integer pos = selectedItems) { mainItems.remove((int)pos); } adapter.notifyDataSetChanged(); }