在网格视图中重复选择多个项目

时间:2018-09-22 19:45:48

标签: android android-layout android-studio gridview multipleselection

This is the first grid where I selected

问题是: when I scroll down, here random images are getting selected

我将滑行库用于网格视图。这是用于多项选择的Java代码:

mgridview.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        @Override
        public void onItemCheckedStateChanged(ActionMode actionMode, int i, long l, boolean b) {
            if (sel_tab2_images.contains(tab2_images.get(i))) {
                sel_count = sel_count - 1;
                actionMode.setTitle(sel_count + " images selected");
                sel_tab2_images.remove(tab2_images.get(i));
                viewprev = mgridview.getChildAt(i - mgridview.getFirstVisiblePosition());
                viewprev.setBackgroundColor(Color.WHITE);
                viewprev.setAlpha(1f);
                if(Build.VERSION.SDK_INT>=23) {
                    viewprev.setForeground(null);
                }


            } else {
                sel_count = sel_count + 1;
                actionMode.setTitle(sel_count + " images selected");
                sel_tab2_images.add(tab2_images.get(i));


                viewprev = mgridview.getChildAt(i - mgridview.getFirstVisiblePosition());
                if (viewprev != null) {

                    viewprev.setAlpha(0.5f);

                  //ImageView imageView=viewprev.findViewById(R.id.tick);

                   //Resource(R.drawable.ic_save_black_24dp);
                    if(Build.VERSION.SDK_INT>=23) {
                        viewprev.setForeground(getResources().getDrawable(R.drawable.ic_check_white_24dp));
                    }







                }
                else
                    Toast.makeText(getContext(), "Error Occured", Toast.LENGTH_SHORT).show();

            }

        }






        @Override
        public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
            MenuInflater inflater = actionMode.getMenuInflater();
            inflater.inflate(R.menu.context_action_bar, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
            int del_count = 0;
            switch (menuItem.getItemId()) {
                case R.id.delete_action_bar:
                    for (String sel : sel_tab2_images) {
                        file = new File(sel);
                        if (file.exists()) {
                            file.delete();
                            tab2_images.remove(sel);
                            if (tab1_fragment.tab1_images.contains(sel)) {
                                tab1_fragment.tab1_images.remove(sel);
                            }
                            if (tab3_fragment.spam_list.contains(sel)) {
                                tab3_fragment.spam_list.remove(sel);
                            }
                            UpdateGallery(sel);
                            /*
                            int pos_1=tab2_images.indexOf(sel);
                            tab2_images.remove(sel);
                            adapter.notifyDataSetChanged();
                            viewprev=mgridview.getChildAt(pos_1);
                            viewprev.setBackgroundColor(Color.WHITE);
                            */

                            del_count++;
                        }
                    }

                    adapter = new ImageAdapterGridView(getContext());
                    mgridview.setAdapter(adapter);
                    Toast.makeText(getContext(), del_count + " images deleted", Toast.LENGTH_SHORT).show();
                    sel_count = 0;
                    sel_tab2_images.clear();
                    actionMode.finish();
                    break;
            }

            return true;


        }

        @Override
        public void onDestroyActionMode(ActionMode actionMode) {
        }
    });





    return view;


}

下面是Grid View的Java代码。我还尝试了@overriding getItemgetItemId方法,但是它不起作用。这是我的第一个应用程序,我仍在学习中。请帮我解决这个问题!

public class ImageAdapterGridView extends BaseAdapter {
    private Context mcontext;

    public ImageAdapterGridView(Context c) {
        mcontext = c;
    }

    public int getCount() {
        return tab2_images.size();
    }

    public Object getItem(int position) {
        return position;
    }

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


    public View getView(int position, View convertview, ViewGroup parent) {
        ImageView imageView;
        if(convertview==null){
            imageView= new ImageView(mcontext);
            imageView.setLayoutParams(new GridView.LayoutParams(280,320));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(16,16,16,16);
        }
        else{
            imageView=(ImageView)convertview;
        }
        Glide.with(tab2_fragment.this).load("file://" + tab2_images.get(position))
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .skipMemoryCache(true)
                .into(imageView);

        return imageView;
    }

}

@Override
public void onResume() {
    super.onResume();
    adapter = new ImageAdapterGridView(getContext());
    mgridview.setAdapter(adapter);
}

}

1 个答案:

答案 0 :(得分:1)

您依赖列表中的视图。在列表视图中,当它们离开屏幕时,它们将被重用。因此,要完成此操作,您必须为每个项目声明一个boolean isSelected(因此,执行此操作的最佳位置是在包含所有每个网格属性的项目模型中),并在用户选择网格项目并使其正确时将其设为true始终根据 布尔值视图更新您的视图。

当前,您的模型仅保存图片的网址。如果您需要在其中保存isSelected布尔值,则应构建模型对象,如:

public class ImageModel {
    public String url;
    public boolean isSelected;
}

然后使用网格列表中的字段对象:

public void onItemCheckedStateChanged(ActionMode actionMode, int i, long l, boolean b) {
    ImageModel image = tab2_images.get(i);
    if (image.isSelected) {
        // Image is selected you need to deselect it
    } else {
        // Image is not selected you need to select it
    }
}

在适配器中:

public View getView(int position, View convertview, ViewGroup parent) {
    // Your view construction code

    ImageModel image = tab2_images.get(i);
    if (image.isSelected) {
        // Show your check image view
    } else {
        // Image is not selected yet, hide your check image view
    }
}

我希望这对您有所帮助,但认为网格视图已被弃用,请尝试考虑使用效率更高,速度更快的RecyclerView