在listview中切换播放暂停ImageView

时间:2017-04-12 08:09:31

标签: android listview adapter android-adapter

我有一个列表视图,其中包含每行中有播放和暂停按钮的歌曲。 显然,我在列表视图中不能同时有两个暂停Icon(两首播放歌曲)所以我需要先重置所有这些以播放Icon然后将所选视图设置为暂停Icon。 我怎样才能做到这一点?

这是我到目前为止所做的:

在模型类(产品)中:

 public boolean paused = true;

 private int PlayPauseId;
   public int getPlayPauseId(){
    return PlayPauseId;
 }
public void setPlayPauseId(int playPauseId) {
PlayPauseId = playPauseId;
}
适配器

中的

public interface PlayPauseClick {
void playPauseOnClick(int position);
}
private PlayPauseClick callback;
public void setPlayPauseClickListener(PlayPauseClick listener) {
    this.callback = listener;
}
.
.
.


    holder.playPauseHive.setImageResource(product.getPlayPauseId());
    holder.playPauseHive.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (callback != null) {
                callback.playPauseOnClick(position);
            }
        }
    });

我的活动中的回调:

@Override
public void playPauseOnClick(int position) {
    final Product product = movieList.get(position);
    if (product.paused) {
        product.setPlayPauseId(R.drawable.ic_pause);
        product.paused=false;
    }else {
        product.setPlayPauseId(R.drawable.ic_play);
        product.paused = true;
    }
    this.adapter.notifyDataSetChanged();
}

1 个答案:

答案 0 :(得分:0)

您必须检查适配器的getView()方法中的条件,如下所示:

    if (product.paused) {
            holder.playPauseHive.setImageResource(R.drawable.ic_play);
        }else {
            holder.playPauseHive.setImageResource(R.drawable.ic_pause);
        }