我正在使用ListView
來顯示播放列表,我使用可點擊的ImageView
來停止/恢復播放,目前我正在編寫自定義適配器。問題是:當我點擊ImageView
時,雖然檢測到點擊事件,但我沒有發生任何事情,因爲我可以在LogCat輸出中看到。什麼做錯了?下面android-在ListView中處理項目點擊
public class AudioListAdapter extends ArrayAdapter<Playlist> {
Context context;
int layoutResourceId;
Playlist data[] = null;
public AudioListAdapter(Context context, int layoutResourceId,
Playlist[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
PlaylistHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new PlaylistHolder();
PlaylistHolder.imgBtnPlay = (ImageView) row
.findViewById(R.id.imgListBtnPlay);
holder.txtTitle = (TextView) row.findViewById(R.id.tvListItemSong);
holder.txtArtist = (TextView) row
.findViewById(R.id.tvListItemArtist);
Playlist playlist = data[position];
holder.txtTitle.setText(playlist.Title);
holder.txtArtist.setText(playlist.Artist);
PlaylistHolder.imgBtnPlay.setImageResource(playlist.btn);
PlaylistHolder.imgBtnPlay
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PlaylistHolder.imgBtnPlay
.setBackgroundResource(R.drawable.list_button_pause);
Log.e("onClick", "image clicked"); //Click events are detected
}
});
row.setTag(holder);
} else {
holder = (PlaylistHolder) row.getTag();
}
return row;
}
static class PlaylistHolder {
static ImageView imgBtnPlay;
TextView txtTitle, txtArtist;
}
}
@ Mover1ck你剛剛改變了圖像點擊按鈕....是不是會發生..? – Pragnani 2013-02-26 17:49:23
按鈕的背景是否在點擊該項目時發生了變化? – Elior 2013-02-26 17:56:28
就是這個問題。儘管點擊事件被系統檢測到,但按鈕背景不會改變 – Droidman 2013-02-26 17:57:01