我有一个列表视图,每行有三个图标。如果我按下一个图标(播放),该图标需要将背景更改为另一个(停止)。然后如果我点击不同行的同一个图标,之前改变的图标需要切换到上一个背景(停止播放),新点击的图标需要改变背景到第二个状态(播放停止) .
这是我正在使用的代码,但似乎无法正常工作。它会更改单击的图标,但如果我单击新行中的相同图标,它不会更改先前单击的行中的图标。
从适配器:
playSong.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying() && mediaPlayer != null) {
mediaPlayer.release();
if (oldPosition != -1) {
((AlbumDetails) activity).changePicToPlay(oldPosition,
playSong);
}
mediaPlayer = new MediaPlayer();
} else {
oldPosition = position;
try {
mediaPlayer.setDataSource(data.get(position)
.getSONG_MP3());
final ProgressDialog bufferingDialog = new ProgressDialog(
activity);
bufferingDialog.setMessage(activity
.getString(R.string.buffering));
bufferingDialog.show();
mediaPlayer.prepareAsync();
mediaPlayer
.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer arg0) {
bufferingDialog.dismiss();
mediaPlayer.start();
}
});
postData(1, position);
mediaPlayer
.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
playSong.setBackgroundResource(R.drawable.play_nr);
mediaPlayer.release();
}
});
((AlbumDetails) activity).changePicToStop(oldPosition);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
从我的活动:
public void changePicToStop(int position) {
View vi = listView.getChildAt(position);
if (vi != null) {
ImageView playSong = (ImageView) vi
.findViewById(R.id.songs_item_play);
playSong.setBackgroundResource(R.drawable.stop);
}
}
public void changePicToPlay(int position, ImageView image) {
View vi = listView.getChildAt(position);
if (vi != null) {
image.setBackgroundResource(R.drawable.play_nr);
listView.refreshDrawableState();
}
}