-1

I have audio player layout as an item in recycler view . I want the play icon to change into pause icon on icon clicked . The log shows that icon has been set but the icon doesn't change in appearance in recycler view .

My code:

@Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {


        customNoteView = notesList.get(i);
        notesText = viewHolder.notesWriteArea;
        notesImage = viewHolder.notesImage;
        notesAudio = viewHolder.notesAudio;
        notesAudioPlayButton = viewHolder.notesAudioPlayButton;
        notesAudioSeekBar = viewHolder.notesAudioSeekBar;
        notesAudioDeleteButton = viewHolder.notesAudioDeleteButton;



        if(customNoteView.getNoteText()!=null){
            editTextList.add(notesText);
            notesText.setVisibility(View.VISIBLE);
            notesImage.setVisibility(View.GONE);
            notesAudio.setVisibility(View.GONE);
            notesText.requestFocus();
            focussedEditText = notesText;
            notesText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if(b){
                        focussedEditText = ((CustomEditText)view);
                        //Toast.makeText(context, ((CustomEditText) view).getText().toString(), Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
        else if(customNoteView.getImageUri()!=0){
            notesImage.setVisibility(View.VISIBLE);
            notesText.setVisibility(View.GONE);
            notesAudio.setVisibility(View.GONE);
            Glide.with(notesImage.getContext())
                    .load(notesImage.getContext().getDrawable(customNoteView.getImageUri()))
                    .into(notesImage);

            notesImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, FullScreen.class);
                    ActivityOptionsCompat options =  ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) context,
                            notesImage,
                            ViewCompat.getTransitionName(notesImage)
                            );

                    context.startActivity(intent, options.toBundle());

                }
            });
        }
        else if(customNoteView.getAudioUri()!=null){
            notesAudio.setVisibility(View.VISIBLE);
            notesImage.setVisibility(View.GONE);
            notesText.setVisibility(View.GONE);
            notesAudioPlayButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    notesAudioPlayButton.setBackground(context.getDrawable(R.drawable.ic_pause));
                    notesAudio.setBackground(context.getDrawable(R.drawable.gray_rectangular_background));
                    Adapter.this.notifyItemChanged(viewHolder.getAdapterPosition());

                    Toast.makeText(context, ""+viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
                }
            });

        }

    }

I actually have a linear layout for audio item. I'm setting onClickListener() no an ImageButton to change it's background from play to pause.

Extra information:

I have observed that clicking the icon first time doesn't do anything .

Clicking it second time changes it to pause icon but then it quickly turns back to play icon .

Clicking it third time changes it to pause icon .

Any suggestion to get this working be great !

Hissaan Ali
  • 1,373
  • 2
  • 12
  • 33

3 Answers3

0

Do you execute the code you showed in Adapter? If so, why don’t you call notifyItemChanged(position)? And if not, why don’t you create method in Adapter that update view with the parameter of position and call it from Activity or Fragment?

  • Do you execute the code you showed in Adapter? I am actually doing that in adapter . If so, why don’t you call notifyItemChanged(position)? please look carefully i am using the same method with the position of the view. – Hissaan Ali Aug 09 '19 at 11:48
  • Do you try removing "Adapter.this.notifyItemChanged(viewHolder.getAdapterPosition());" and change following code "notifyItemChanged(position)? – Shohei Maeno Aug 09 '19 at 13:12
0

delete this line :

Adapter.this.notifyItemChanged(viewHolder.getAdapterPosition());

or save current background icon for recyclerview item

0

I'm always using notifyDataSetChanged() and never had any problem with that. Can you just use that or are there certain reasons you doing it otherwise?

Jones
  • 101
  • 7
  • i am pretty sure you were lucky then or had a different case . it doesn't work for me . can you suggest something else ? – Hissaan Ali Aug 09 '19 at 11:58