Android ListView 子视图 setEnabled() 和 setClickable() 什么都不做 [英] Android ListView child View setEnabled() and setClickable() do nothing
问题描述
在用户单击我的 ListView
中的项目后,我正在执行一些 AsyncTask
工作.我想禁用该项目,以便不能单击两次.我已将点击侦听器简化为仅包含此方法,但它仍然没有为我做任何事情,视图看起来相同并且它让自己再次愉快地点击,这让我很烦恼.
I'm doing some AsyncTask
work after user clicks an item in my ListView
. I'd like to disable the item so it can't be clicked twice. I've simplified the click listener to contain only this method, but it still doesn't do anything for me, the view looks the same and it lets itself be happily clicked again, much to my annoyance.
public void onItemClick(AdapterView<?> parent, View clickedView,
int position, long id) {
item = (Episode) parent.getItemAtPosition(position);
clickedView.setClickable(false);
clickedView.setEnabled(false);
clickedView.invalidate();
}
我对每一行的视图是一个自定义的 LinearLayout
,带有两个 TextView
.
My View for each row is a custom LinearLayout
with two TextView
s.
推荐答案
因此,您可能也在使用自定义适配器.如果这样做,请覆盖这些方法:
So, you may be using a custom adapter too. If you do, override these methods:
public boolean areAllItemsEnabled() {
return false;
}
public boolean isEnabled(int position) {
// return false if position == position you want to disable
}
然后,当您收到点击时,告诉适配器最后点击的项目是什么,并在该位置的 isEnabled
上返回 false.例如,您可以在您的适配器中使用这样的方法:
Then, when you receive a click tell the adapter what was the last item clicked and return false on isEnabled
for that position. For instance, you can have a method like this in your adapter:
private int mLastClicked;
public void setLastClicked(int lastClicked){
mLastClicked = lastClicked;
}
这篇关于Android ListView 子视图 setEnabled() 和 setClickable() 什么都不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!