Android – change the image in ImageView in recyclerview

I want to change the image of ImageView in recyclerview

For example:

I have 30 items in recyclerview. When I click the item at position 1, the item changes the image from play to pause. Then when I scroll down to position 15 and click the play button, I select the previous button (item 1) to change the image back to play state, and the image at item 15 should be changed to pause. I have implemented onclicklistener inside onbindviewholder. However, It changed the image in the wrong project. Please help me

if (mediaPlayer != null) {
    mediaPlayer.reset();
    mediaPlayer.stop();
    LinearLayout view = (LinearLayout) recyclerView.getChildAt(pos);
    ImageView button = (ImageView) view.findViewById(R.id.playbutton);
    button.setImageResource(R.drawable.ic_play_arrow_black_24dp);
}




  playbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isPlaying = sharedPreferences.getBoolean(ConstantValue.ISPLAYING, false);
                RecordingDetail recordingDetail = list.get(getAdapterPosition());
                if (!isPlaying) {
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putBoolean(ConstantValue.ISPLAYING, true);
                    editor.putInt(ConstantValue.CURRENTINDEX, getAdapterPosition());
                    editor.commit();
                    playbutton.setImageResource(R.drawable.ic_pause_black_24dp);
                    mediaPlayerControl.Play(recordingDetail.path, getAdapterPosition(), progressBar);
                }

The play button here is the image view I want to change, and mediaplayercontrol is the interface

resolvent:

You can use global fields to track the selected location, and then check whether the current view is selected in onbindviewholder. In addition, I strongly recommend that you assign onclicklistener within viewholder

>Declare a global variable:

private int selectedPosition = -1;

> then set the selected location in onClick, and then call notifyDatasetChanged:

@Override
public void onClick(View view) {
    selectedPosition = getAdapterPosition();    

    if (mediaPlayer != null) {
        mediaPlayer.reset();
        mediaPlayer.stop();
    }

    RecordingDetail recordingDetail = list.get(selectedPosition);            

    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(ConstantValue.ISPLAYING, true);
    editor.putInt(ConstantValue.CURRENTINDEX, selectedPosition);
    editor.commit();

    mediaPlayerControl.Play(recordingDetail.path, selectedPosition, progressBar);

    notifyDatasetChanged();
}

>Finally, check the selected position in the onbindviewhandler and set the appropriate image:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    LinearLayout view = holder.linearLayout;
    ImageView button = (ImageView) view.findViewById(R.id.playbutton);

    if (position == selectedPosition) {
        button.setImageResource(R.drawable.ic_pause_black_24dp);
    } else {
        button.setImageResource(R.drawable.ic_play_arrow_black_24dp);
    }
}

edit

As @ shalafi suggests, you should not keep references to specific views within the recyclerview adapter because views have been recycled (reused)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>