2011-04-04 46 views
14

所以我在适配器下面的代码:机器人 - 禁用ListView项点击重新启用它

@Override 
    public boolean isEnabled(int position) 
    { 
     GeneralItem item = super.getItem(position); 
     boolean retVal = true; 


      if (item != null) 
      { 
       if (currSection != some_condition) 
       retVal = !(item.shouldBeDisabled()); 
      } 
     return retVal; 
    } 


    public boolean areAllItemsEnabled() 
    { 
     return false; 
    } 

这里的问题:如果我在最初的结合禁用我的项目,我现在引发事件在屏幕上,无论如何都需要启用它们。该行为执行后,我是否再次重新绑定它?

例如:

onCreate{ 

// create and bind to adapter 
// this will disable items at certain positions 

} 

onSomeClick{ 

I need the same listview with same items available for click no matter what the conditions of positions are, so I need them all enabled. What actions should I call on the adapter? 

} 

的问题是,我可以有一个很长的列表视图太。它假设支持6000个项目。所以重新绑定肯定不是一种选择。

感谢,

回答

25

怎么样让你的适配器上的实例变量:

boolean ignoreDisabled = false; 

然后在areAllItemsEnabled

public boolean areAllItemsEnabled() { 
    return ignoreDisabled; 
} 

,然后在isEnabled开头:

public boolean isEnabled(int position) { 
    if (areAllItemsEnabled()) { 
     return true; 
    } 
    ... rest of your current isEnabled method ... 
} 

然后,您可以通过适当地设置ignoreDisabled并在ListView上调用invalidate在两种模式之间切换。

请注意,isEnabled的添加可能不需要;它似乎更完整一点。

+0

是的,这正是我如何解决它:)伟大的答案谢谢你! – dropsOfJupiter 2011-04-05 16:44:55