I am in stuck with pretty easy thing.
I have an activity with Fragment which includes RecyclerView and custom adapter with items. This activity is about radio stations.
So in my adapter I have several items with Play-Stop button. When I click on the button there is "play" symbol. One more click on those button - there will be "stop" button. This logic works.
BUT if I will click on other item, on other Play-Stop button, all previously clicked items will save different states, so I want to change state for previous clicked item - only current clicked item with current position can have different Play-Stop button. How can I get it?
I have tried some notify...()
methods but without result that I want. I think I am trying notify adapter in the wrong places.
holder.LLPLayStop.setOnClickListener(v->{
if (isPlay[0] == false) {
isPlay[0] = true;
try {
if (mp != null) {
mp.stop();
mp.release();
}
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource("http://cast.radiogroup.com.ua:8000/avtoradio");
mp.prepareAsync();
// notifyDataSetChanged();
holder.PBLoading.setVisibility(View.VISIBLE);
holder.list_item_play_stop.setVisibility(View.GONE);
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
holder.PBLoading.setVisibility(View.GONE);
holder.list_item_play_stop.setVisibility(View.VISIBLE);
holder.list_item_play_stop.setImageDrawable(mContext.getResources().getDrawable(R.drawable.radio_stop));
}
});
} catch (IOException e) {
Log.e(TAG, "prepare() failed");
}
} else {
isPlay[0] = false;
mp.stop();
mp.release();
mp = null;
// notifyDataSetChanged();
holder.list_item_play_stop.setImageDrawable(mContext.getResources().getDrawable(R.drawable.radio_play));
}
// notifyItemChanged(position);
});
you need to keep playing index and check each time on click, for all rows. so you need variable to keep clicked row, like this:
in your adapter:
private Integer selectedPos = -1;
in onClick
of play/stop button:
{
selectedPos = position;
notifyDataSetChanged(); // notify list , then each row can handle it's state base on selectedPos
}
and finally check playing row in onBindViewHolder
:
if(selectedPos == position){ // position is the current row position
//this row is selected for playing...
//do appropriate works
} else{
//this row is not select for play so change button style to normal
}