【问题标题】:Disable listview items with selector and isEnabled()使用选择器和 isEnabled() 禁用列表视图项
【发布时间】:2011-07-19 09:15:39
【问题描述】:

范围: - 使用覆盖的 ArrayAdapter; - 使用选择器; - 使用 isEnabled 禁用项目。

目标: - 禁用一些列表项并通过选择器加载禁用状态视图。

问题: - 一切正常(自定义视图、未聚焦、聚焦和按下状态的选择器),但禁用的项目不使用禁用状态的选择器。

正在调查:当我使用 isEnabled 禁用列表视图中的某些项目时,层次结构查看器显示禁用的项目无法聚焦、无法点击但(!)已启用。

这是一个错误还是缺少什​​么?

附:实际上,文档说 isEnabled 不会对列表项执行 setEnabled(false) ,它使它成为一个 divider(?) 对象。 P.P.S 我还尝试使用 if 语句将我的视图(在 getView 中)设置为 isEnabled(false)。但它只适用于重点项目?

我的选择器看起来像:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Disabled -->
    <item 
        android:state_enabled="false"
        android:textColor="@color/greyDark"
        android:drawable="@drawable/list_item_disabled" />
    <!-- Pressed -->
    <item 
        android:state_enabled="true"
        android:state_pressed="true"
        android:textColor="@android:color/white"
        android:drawable="@drawable/list_item_pressed" />
    <!-- Focused -->
    <item
        android:state_enabled="true"
        android:state_focused="true"
        android:textColor="@android:color/white"
        android:drawable="@drawable/list_item_focused" />
    <!-- Default -->
    <item 
        android:state_enabled="true"
        android:drawable="@drawable/list_item_unfocused" />
</selector>

【问题讨论】:

    标签: android xml listview android-arrayadapter


    【解决方案1】:

    适配器中的isEnabled()函数只会使item无法聚焦和无法点击。 您需要在 adapter.getView() 末尾调用 view.setEnabled() 以使您的选择器正常工作。

    另外,要让父视图将启用状态传递给其后代,您需要在 xml 文件中为子视图指定属性 android:duplicateParentState="true"

    【讨论】:

    • 未来读者请注意:不要像我做的那样假设我只需要第一部分,因为我的 XML 布局文件中没有嵌套布局!
    【解决方案2】:

    破解: 使用 getView 检查禁用的项目逻辑并使用另一个布局膨胀视图。 isEnabled 还是有用的。

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row;
    
        if (mListItem[position].isEnabled() == false) row = inflater.inflate(
                R.layout.list_row_disabled, null);
        else {
            row = inflater.inflate(R.layout.list_row, null);
            // set right extensible icon
            if (mListItem[position].getType()) {
                ImageView ic_arrow = (ImageView) row.findViewById(R.id.list_row_arrow);
                ic_arrow.setImageResource(R.drawable.ic_arrow_right);
            }
        }
        // set left icon
        ImageView ic_item = (ImageView) row.findViewById(R.id.list_row_icon);
        ic_item.setImageResource(mListItem[position].getIcon());
        // blend icon if item is disabled
        if (mListItem[position].isEnabled() == false) 
            ic_item.setColorFilter(0x99D0D0D0,Mode.SRC_ATOP); // make icons look grayer 
    
        // set title text
        TextView txvTitle = (TextView) row.findViewById(R.id.list_row_title);
        txvTitle.setText(mListItem[position].getTitle());
    
        return row;
    }
    
    @Override
    public boolean isEnabled(int position) {
        return mListItem[position].isEnabled();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-03
      • 2018-11-08
      • 1970-01-01
      • 2013-11-09
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多