Android 在 listview 的 list item 中禁用 onclick 事件
问题描述:
在 Listview 中有一个 HeaderView。当点击它时,它隐藏文本,然后显示一个 spinner 来从别的地方获取数据。
第一次点击后,我想禁用 onClick,那样的话就不能调用多次获取。
我使用 v.setClickable(false)
和 v.setEnabled(false)
, 但是都不能正常运行。
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (position == 0) {
ProgressBar pb = (ProgressBar) v
.findViewById(R.id.refresh_progress);
pb.setVisibility(View.VISIBLE);
TextView tv = (TextView) v.findViewById(R.id.load);
tv.setVisibility(View.GONE);
v.setClickable(false);
DownloadTask dt = new DownloadTask(v, "Old Message");
dt.execute();
}
}
有什么好的建议吗?谢谢!
答
private HashMap<Integer,Boolean> map = new HashMap<Integer,Boolean>();
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (position == 0 && ! map.get(position)) {
map.put(position,true);
ProgressBar pb = (ProgressBar) v
.findViewById(R.id.refresh_progress);
pb.setVisibility(View.VISIBLE);
TextView tv = (TextView) v.findViewById(R.id.load);
tv.setVisibility(View.GONE);
v.setClickable(false);
DownloadTask dt = new DownloadTask(v, "Old Message");
dt.execute();
}
}
答
private boolean wasFetchStarted = false;
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (position == 0 && ! wasFetchStarted) {
wasFetchStarted = true;
ProgressBar pb = (ProgressBar) v
.findViewById(R.id.refresh_progress);
pb.setVisibility(View.VISIBLE);
TextView tv = (TextView) v.findViewById(R.id.load);
tv.setVisibility(View.GONE);
v.setClickable(false);
DownloadTask dt = new DownloadTask(v, "Old Message");
dt.execute();
}
}