【发布时间】:2021-06-02 16:11:41
【问题描述】:
我陷入了非常简单的事情。
我有一个片段活动,其中包括 RecyclerView 和带有项目的自定义适配器。 此活动与广播电台有关。
所以在我的适配器中,我有几个带有播放停止按钮的项目。当我单击按钮时,会出现“播放”符号。再单击这些按钮 - 将出现“停止”按钮。这个逻辑有效。
但是如果我要点击其他项目,在其他播放停止按钮上,所有之前点击的项目将保存不同的状态,所以我想改变之前点击项目的状态 - 只有当前位置的当前点击项目可以有不同的播放- 停止按钮。 我怎样才能得到它?
我尝试了一些notify...() 方法,但没有我想要的结果。 我想我在错误的地方尝试通知适配器。
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);
});
【问题讨论】:
-
isPlay[0] 值来自哪里?@danyapd
-
您能否再澄清一下
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.其次,由于 RecyclerView 回收了该项目,因此您的状态可能会混淆。
标签: android android-recyclerview android-mediaplayer android-arrayadapter