我正在使用 aListView
来显示播放列表,并使用可点击ImageView
来停止/恢复播放,目前我正在编写一个自定义适配器。问题:当我点击 时ImageView
,尽管在 LogCat 输出中可以看到点击事件被检测到,但什么也没有发生。做错了什么?适配器代码如下
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;
}
}