如何解决单击其他项目时更新适配器中的项目
我陷入了非常简单的事情。
我有一个 Fragment 活动,其中包括 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);
});
解决方法
您需要继续播放索引并在每次点击时检查所有行。所以你需要变量来保持点击行,就像这样:
在您的适配器中:
private Integer selectedPos = -1;
在播放/停止按钮的 onClick
处:
{
selectedPos = position;
notifyDataSetChanged(); // notify list,then each row can handle it's state base on selectedPos
}
最后检查 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
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。