【问题标题】:android - disable Listview item click and re-enable itandroid - 禁用 Listview 项目单击并重新启用它
【发布时间】:2011-04-04 18:59:34
【问题描述】:

所以我在适配器中有以下代码:

@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个项目。所以重新绑定它当然不是一种选择。

谢谢,

【问题讨论】:

    标签: android listview adapter


    【解决方案1】:

    在你的适配器上有一个实例变量怎么样:

    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 的添加可能是不需要的;它只是看起来更完整一些。

    【讨论】:

    • 是的,这正是我解决它的方法 :) 很好的答案,谢谢!
    猜你喜欢
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多