为了在创建列表时禁用列表项,您必须从继承
ArrayAdapter。您必须重写以下方法:
isEnabled(intposition)和
areAllItemsEnabled()。在以前,您返回
true或
false取决于列表项在给定位置是否启用。在后者中,您返回
false。
如果要使用
createFromResource(),则还必须实现该方法,因为
ArrayAdapter.createFromResource()静态方法仍将实例化
ArrayAdapter而不是您自己的适配器。
最后,代码如下所示:
class MenuAdapter extends ArrayAdapter<CharSequence> { public MenuAdapter( Context context, int textViewResId, CharSequence[] strings) { super(context, textViewResId, strings); } public static MenuAdapter createFromResource( Context context, int textArrayResId, int textViewResId) { Resources resources = context.getResources(); CharSequence[] strings = resources.getTextArray(textArrayResId); return new MenuAdapter(context, textViewResId, strings); } public boolean areAllItemsEnabled() { return false; } public boolean isEnabled(int position) { // return false if position == position you want to disable }}


