Search code examples
androidgridviewlisteneronclicklistenermultichoiceitems

onClickListener in getView() method messes up MultiChoiceModeListener()


So I have a GridView that implements the multiple choice mode listener , and every time the user taps the item it should change it's background image ; and when he/she long taps , the multi choice toolbar should appear.

However since I have on click listener in getView() it somehow blocks the other one.

(if I remove the listener from getView() , the other one works just fine)

Any advices ?

Here's my code:

MultiChoiceListener:

 gView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
        checkedPos = new SparseBooleanArray();

        gView.setMultiChoiceModeListener(new GridView.MultiChoiceModeListener() {
            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {

                currentArray = gView.getCheckedItemPositions();
                int itemCount = gView.getCheckedItemCount();

                switch (itemCount){
                    case 1:
                        mode.setSubtitle("One item selected.");
                        break;
                    default:
                        mode.setSubtitle(itemCount + " items selected.");
                        break;
                }
...

getView():

 convertView.setLongClickable(true);
        final Holder finalHolder = holder;
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!pressed) {
                    finalHolder.img.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_pause_bg));
                    pressed = true;
                }
                else{
                    finalHolder.img.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_noise_bg));
                    pressed = false;
                }
            }
        });

Thank you for your time!


Solution

  • Nevermind, I worked it out. I'm going to leave my answer if someone needs a solution for the same problem.

    First of all forget about the listener in the getView() method , instead go where you have the code for your MultiChoiseModeListener and call setOnItemClickListener() for your gridView -> using the id of the image you want to change constantly:

    gView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    view.setSelected(true);
    
                    ImageView image = (ImageView) view.findViewById(R.id.noise_image); //use the id given in your layout
    
                    if(!itemPressed.get(position)) {
                        image.setBackground(ContextCompat
                                .getDrawable(mContext, R.drawable.ic_running));
                        itemPressed.put(position,true);
                    }
                    else{
                        itemPressed.put(position,false);
                        image.setBackground(ContextCompat.getDrawable(mContext,R.drawable.ic_normal));
                    }
    

    Doing so, both listeners will work.

    Notice that itemPressed is a Map in order to memorize which item was clicked so when changing background images , there is no confusion.