-3

I'm creating an application for Android, and in this moment i want just create a selectable list using a RecyclerView, for this, when a user clicks on an item, I put its id in an array and I change its icon. The problem is that, when I scroll down and then I scroll up again or when I change fragment, the array still contains all the id of the items, but the icons comes back as before, they don't mantain the new image that I set.

This is the onBindViewHolder metod, this method is the only in the whole project which change the imageview :

 List<Integer> selected = new LinkedList<>();
 @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mBoundString = mValues.get(position);
        holder.mTextView.setText(mValues.get(position));
        final int pos = position;
        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Context context = v.getContext();
                Animation to_middle = AnimationUtils.loadAnimation(v.getContext(), R.anim.to_middle);
                final Animation from_middle = AnimationUtils.loadAnimation(v.getContext(), R.anim.from_middle);
                to_middle.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        holder.mImageView.setImageResource(R.drawable.ic_action_navigation_check);
                        holder.mImageView.clearAnimation();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });

                holder.mImageView.setAnimation(to_middle);
                holder.mImageView.startAnimation(to_middle);
                selected.add(pos);

            }
        });


        if(!selected.contains(position)) {

            /* I checked and this method it's not called when 
               an item is selected, so the following instruction 
               isn't the cause of my problem, anyway the image
               comes back as before */
            Glide.with(holder.mImageView.getContext())
                    .load(R.drawable.ic_default)
                    .fitCenter()
                    .into(holder.mImageView);
        }
    }

I'm sure that R.drawable.ic_default isn't used elsewhere, so which method is changing the image? Thank you

  • I was able to use this tutorial to create multi-select recyclerview with background as state-list drawable this might help you if i understood your question right.. https://www.bignerdranch.com/blog/recyclerview-part-2-choice-modes/ – dlohani Jul 28 '15 at 13:03
  • http://stackoverflow.com/q/7738527/2389078 – DroidDev Jul 28 '15 at 13:04
  • I don't want to change my whole project only for this marginal feature, it would mean that polymorphism is dead. I just want that icon flips when I click on a item (like gmail app, the animation in my code does this) and that it stays flipped even if I scroll down. Thank you anyway – Christian Vincenzo Traina Jul 28 '15 at 13:16

1 Answers1

0
if(selected.contains(position)) { // Check if already selected


        Glide.with(holder.mImageView.getContext())
                .load(R.drawable.ic_action_navigation_check) // set selected drawable here.
                .fitCenter() 
                .into(holder.mImageView);
    } 

You are checking that "if not selected set image to default" but you need to check "if selected set image to selected image".

Sachin Chandil
  • 17,133
  • 8
  • 47
  • 65
  • Ok, it was a bit more difficult but the logical principle it's right, now it seems work. I still don't understand why if I scrolled down, the previous icon comes back. It's a strange behavior, if it crashed or if it put a white image I would have noticed the error. Anyway there isn't any there was no reason to put negative ratings to the question, the community is born for this and these questions are allowed in the regulations – Christian Vincenzo Traina Jul 28 '15 at 13:30
  • Whenever recyclerview is scrolled down its non visible vies gets removed from memory. And when again scrolled up those views are again created. That's why this behavious occirs. – Sachin Chandil Jul 28 '15 at 14:17
  • Yes but, if the view is again created but this time its id is in the array, I expect a blank icon, not the previous icon – Christian Vincenzo Traina Jul 29 '15 at 14:52