Android的ListView的子View的setEnabled()和setClickable()什么都不做什么都不、ListView、Android、View

2023-09-12 22:06:43 作者:取决于你i

我正在做一些AsyncTask的工作,用户点击我的ListView的项目之后。我想禁用的项目,因此它不能被点击两次。我已经简化了点击监听器只包含此方法,但它仍然不为我做任何事情,视图看起来相同,它可以让自己愉快地再次点击,这让我的烦恼。

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有两个TextViews。

My View for each row is a custom LinearLayout with two TextViews.

推荐答案

所以,你可以使用自定义适配器了。如果你这样做,覆盖这些方法:

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
}

然后,当您收到点击告诉适配器什么是最后单击的项目,并在该位置上返回false的的IsEnabled 。例如,你可以有这样的方法在你的适配器:

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;
}