如何更改不是我在RecycleView中单击的项目?

时间:2019-05-18 01:57:18

标签: java android android-recyclerview onclicklistener

摘要

我在RecycleView中有一个CardView,其中有一个按钮可以播放或停止播放一首歌曲,一次只能播放一首歌曲。因此,当我单击其中一个按钮时,所有其他按钮都应具有“播放”图标,而单击的按钮应具有“停止”图标。基本上,我需要更改未单击的项目的图像。

问题

该项目会编译,当同时播放多个歌曲时,歌曲不会重叠,当我单击一个图标时,它会更改为另一个图标,但是如果我单击另一个项目,则单击的前一个不会改变并保持原样相同。

尝试

因此,我尝试遍历RecycleView的所有元素,将其更改回默认图标,然后再更改单击的图标,但是我无法真正访问我没有使用的其他项目点击。我尝试使用全局变量引用上一个项目,但同样,我无法真正处理上一个项目。

AudioGridActivity代码:

public class AudioGridActivity1 extends AppCompatActivity {
private RecyclerView recyclerView2;
private MyAdapter2 adapter2;
private RecyclerView.LayoutManager layoutManager;

private ArrayList<CardAudioGrid> mList;

private MediaPlayer song;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_audio_grid1);
    createList();
    buildRecyclerView();

    final ImageView mediaPlayer = (ImageView) findViewById(R.id.mediaPlayerId); // this is the play/stop "button"

    adapter2.setOnItemClickListener(new MyAdapter2.OnItemClickListener() {
        @Override
        public void onPlayClick(int position) {
            switch(position){
                case 0:
                    pauseTheme();
                    song = MediaPlayer.create(AudioGridActivity1.this, R.raw.gato);
                    playTheme();
                    break;
                case 1:
                    pauseTheme();
                    song = MediaPlayer.create(AudioGridActivity1.this, R.raw.gato);
                    playTheme();
                    break;
                case 2:
                    pauseTheme();
                    song = MediaPlayer.create(AudioGridActivity1.this, R.raw.gato);
                    playTheme();
                    break;
                case 3:
                    pauseTheme();
                    song = MediaPlayer.create(AudioGridActivity1.this, R.raw.gato);
                    playTheme();
                    break;
                case 4:
                    pauseTheme();
                    song = MediaPlayer.create(AudioGridActivity1.this, R.raw.gato);
                    playTheme();
                    break;
                case 5:
                    pauseTheme();
                    song = MediaPlayer.create(AudioGridActivity1.this, R.raw.gato);
                    playTheme();
                    break;


            }

        }
    });

}

public void createList(){
    mList = new ArrayList<>();
    mList.add(new CardAudioGrid(R.drawable.mediaplay2, R.color.colorAccent, ""));
    ...
    mList.add(new CardAudioGrid(R.drawable.mediaplay2, R.color.colorPrimary, ""));
}

public void buildRecyclerView() {
    recyclerView2 = findViewById(R.id.recyclerViewId2);
    recyclerView2.setHasFixedSize(true);
    layoutManager = new GridLayoutManager(this, 2);
    recyclerView2.setLayoutManager(layoutManager);
    adapter2 = new MyAdapter2(this, mList);
    recyclerView2.setAdapter(adapter2);
}
public void playTheme(){
    if(song!=null) song.start();
}
public void pauseTheme(){
    if(song!=null && song.isPlaying()) song.release();
}
...}

适配器代码:

public class MyAdapter2 extends RecyclerView.Adapter<MyAdapter2.MyViewHolder>{
private Context mContext;
private List<CardAudioGrid> mList;
private OnItemClickListener mListener;

public interface OnItemClickListener{
    void onPlayClick (int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
    mListener = listener;
}

public MyAdapter2(Context mContext, List<CardAudioGrid> mList) {
    this.mContext = mContext;
    this.mList = mList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    View view = inflater.inflate(R.layout.audiocard, viewGroup, false);
    return new MyViewHolder(view, mListener);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    holder.audioCardBackground.setImageResource(mList.get(position).getCardBackground());
    ...
    holder.mediaPlayer.setImageResource(mList.get(position).getPlayButton());
}
@Override
public int getItemCount() {
    return mList.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder{
    ImageView mediaPlayer, audioCardBackground;

    public MyViewHolder(View itemView, final OnItemClickListener listener) {
        super(itemView);

        mediaPlayer = (ImageView) itemView.findViewById(R.id.mediaPlayerId);
        audioCardBackground = (ImageView) itemView.findViewById(R.id.cardbackgroundId);
        mediaPlayer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION){
                        listener.onPlayClick(position);
                        mediaPlayer.setImageResource(R.drawable.mediapause2);
                    }else{

                    }
                }
            }
        });
    }

}
}

我应该如何解决这个问题?

我目前正在学习如何正确管理android,因此,如果您解释这些步骤的解决方案,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

在您的CardAudioGrid模型中,初始化一个布尔变量isPlaying = false

在RecyclerViewAdapter MyAdapter2内部 声明一个整数变量,该变量将保留当前播放项目的位置。

int nowPlaying = -1;

单击某个项目后,根据需要更改该项目的isPlayingtrue/false

检查nowPlaying的值,如果其值大于-1则使最后一个模型的isPlaying = false

并分配nowPlaying新的点击位置值。

并致电notifyDataSetChanged();

onBindViewHolder不要忘记根据dataModel设置资源

if(mList.get(position).isPlaying())
    holder.mediaPlayer.setImageResource(mList.get(position).getPlayButton());
else
   holder.mediaPlayer.setImageResource(mList.get(position).getStopButton());

您也可以在不向CardAudioGrid Model类添加新成员的情况下实现此目的。在那种情况下,不用检查isPlaying检查

if(position == nowPlaying)
   holder.mediaPlayer.setImageResource(mList.get(position).getPlayButton());
else
   holder.mediaPlayer.setImageResource(mList.get(position).getStopButton());