我正在使用ListView
来显示播放列表,我使用可点击的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;
}
}
答案 0 :(得分:0)
使用setImageRessource()
代替setBackgroundResource()
来解决问题无法解释但现在可以正常使用
答案 1 :(得分:0)
您可以使用adaptername.notifyDatasetchanged方法刷新列表。但不幸的是,这也将无法工作,因为当点击事件将被触发时,列表无法知道单击了哪个视图。要么你可以尝试使用项目点击监听器,它将为你提供点击项目的位置