Java Android:如何在创建列表时禁用列表项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2183610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:30:39  来源:igfitidea点击:

Android: How to disable list items on list creation

javaandroidlistlistview

提问by Martyn

I'm pretty new to Android dev and still working out a lot of things.

我对 Android 开发人员还很陌生,但仍在做很多事情。

I've got a main menu showing using the following code, but can't work out how to disable selected items in the menu. Can anybody help me with some sample code?

我有一个显示使用以下代码的主菜单,但无法弄清楚如何禁用菜单中的选定项目。有人可以帮我提供一些示例代码吗?

public class listTest extends ListActivity {

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        setListAdapter(ArrayAdapter.createFromResource(this, R.array.mainMenu,
                android.R.layout.simple_list_item_1)); 
        //not sure how to disable list items here
    }

    protected void onListItemClick(ListView list, View view, int position, long id) {
        // can disable items when they are clicked on
        view.setEnabled(false);
    }   

}

and I have a string-arrayin my strings.xml file:

string-array我的 strings.xml 文件中有一个:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="mainMenu">
        <item>Item 1</item>
        <item>Item 2</item>
        <item>Item 3</item>
    </string-array> 
</resources>

Thank you

谢谢

采纳答案by Viktor Bre?an

In order to disable list items on list creation you have to subclass from ArrayAdapter. You have to override the following methods: isEnabled(int position)and areAllItemsEnabled(). In former you return trueor falsedepending is list item at given position enabled or not. In latter you return false.

为了在创建列表时禁用列表项,您必须从ArrayAdapter. 您必须覆盖以下方法:isEnabled(int position)areAllItemsEnabled()。以前,您返回truefalse取决于是否启用给定位置的列表项。在后者你返回false

If you want to use createFromResource()you will have to implement that method as well, since the ArrayAdapter.createFromResource()still instantiates ArrayAdapterinstead of your own adapter.

如果您想使用,createFromResource()您还必须实现该方法,因为ArrayAdapter.createFromResource()仍然实例化ArrayAdapter而不是您自己的适配器。

Finally, the code would look something like the following:

最后,代码将类似于以下内容:

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

回答by Matthias

I believe whether a list item is enabled or not is part of that item's state, so I guess you have to manage that in your ListAdapter. When subclassing an adapter, you can override isEnabled(position). For any position you return true here, the ListView will mark this item as disabled.

我相信列表项是否启用是该项目状态的一部分,所以我猜你必须在你的ListAdapter. 子类化适配器时,您可以覆盖isEnabled(position). 对于您在此处返回 true 的任何位置,ListView 都会将此项目标记为禁用。

So what you want to do is something like this:

所以你想做的是这样的:

class MenuAdapter extends ArrayAdapter<String> {

    public boolean isEnabled(int position) {
       // return false if position == positionYouWantToDisable
    }

}

This probably requires e.g. a Mapmanaging the enabled state of each item if you want to be able to enable/disable an item using a setter.

Map如果您希望能够使用 setter 启用/禁用项目,则这可能需要例如管理每个项目的启用状态。

Then set the custom adapter on your ListView.

然后在 ListView 上设置自定义适配器。

回答by Raginmari

You can disable a list item (= make it not respond to touches) by calling both

您可以通过调用两者来禁用列表项(= 使其不响应触摸)

setClickable(false)

and

setFocusable(false)

in your adapter, for example.

例如,在您的适配器中。

By default, this is not automatically reflected graphically, though.

但是,默认情况下,这不会自动以图形方式反映。

I am currently using that in a list whose list items are not clickable but most of which contain clickable widgets. Works well.

我目前在一个列表中使用它,其列表项不可点击,但其中大部分包含可点击的小部件。效果很好。

This way, list items are drawn normally including the separator (see Janusz' reply to the accepted answer above).

这样,通常会绘制包含分隔符的列表项(请参阅 Janusz 对上述已接受答案的回复)。

回答by CoDe

Or in simple way to un-register and register OnItemClickListenercan be a better idea.

或者以简单的方式取消注册和注册OnItemClickListener可能是一个更好的主意。

To register:

注册:

lstDevice.setOnItemClickListener(context);

lstDevice.setOnItemClickListener(context);

To de-register:

注销:

lstDevice.setOnItemClickListener(null);

lstDevice.setOnItemClickListener(null);