【问题标题】:Update items in adapter when other item clicked单击其他项目时更新适配器中的项目
【发布时间】: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


【解决方案1】:

您需要继续播放索引并在每次点击时检查所有行。所以你需要变量来保持点击的行,像这样:

在您的适配器中:

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
 }

【讨论】:

  • 并不像我想象的那么容易,但最终它完美运行,谢谢。对于其他人:我需要从我的点击侦听器if(selectedPos == position){ 逻辑中取出并将其包装在我的if(isPlay[position]) 逻辑中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-02
  • 1970-01-01
  • 1970-01-01
  • 2019-04-22
  • 1970-01-01
相关资源
最近更新 更多