简体   繁体   English

禁用单击ListView的项目

[英]Disabled click Item of ListView

I am using SimpleAdapter to bind ListView . 我正在使用SimpleAdapter绑定ListView Now, When User Clicks on Item, I want to disable that time from Click() event. 现在,当用户单击项目时,我想从Click()事件中禁用该时间。 I found some tutorial for isEnabled() but I am not understanding that how to use it ? 我找到了isEnabled()一些教程,但不了解如何使用它?

Please help me to solve this problem. 请帮我解决这个问题。 I am using Custom ListView . 我正在使用Custom ListView
This below code disables ListView . 下面的代码禁用ListView

SimpleAdapter adapter = new SimpleAdapter(this, arrlist,
            R.layout.topicwisequestion, new String[] { "option" },
            new int[] { R.id.option }) {

        public boolean areAllItemsEnabled() {
            return ignoreDisabled;
        }

        public boolean isEnabled(int position) {
            if (areAllItemsEnabled()) {
                return true;
            }
            return false;
        }
    };
    lvTWOptions.setAdapter(adapter);

    lvTWOptions.setOnItemClickListener(new OnItemClickListener() {
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              // Code...   
         }
    });

Do it like this. 像这样做。

define a boolean variable 定义一个布尔变量

boolean ignoreDisabled = false;

Then in areAllItemsEnabled: 然后在areAllItemsEnabled中:

public boolean areAllItemsEnabled() {
    return ignoreDisabled;
}

and then at the beginning of isEnabled: 然后在isEnabled的开头:

public boolean isEnabled(int position) {
    if (areAllItemsEnabled()) {
        return true;
    }
}

or else second way is define following code in your adapter class 否则,第二种方法是在适配器类中定义以下代码

private int mLastClicked;
public void setLastClicked(int lastClicked){
    mLastClicked = lastClicked;
}

and then 接着

public boolean areAllItemsEnabled() {
    return false;
}

public boolean isEnabled(int position) {
    // return false if position == position you want to disable
}

add position of your last click event with isEnabled method. 使用isEnabled方法添加最后一次点击事件的位置。

For further reference you can check this link 有关更多参考,您可以检查此链接

Hope this is gonna help you. 希望这会对您有所帮助。

Do it like this. 像这样做。

ListAdapter adapter = new SimpleCursorAdapter(MyList, Layout, c,
new String[] { "Name", "Score" }, to)
{
public boolean areAllItemsEnabled()
{
return false;
}

public boolean isEnabled(int position)
{
return false;
}
};

You'll have to extend the simple adapter, maintain a list of indexes that have been clicked on, implement isEnabled(int) and check if the integer passed is in your list. 您将必须扩展简单适配器,维护已单击的索引列表,实现isEnabled(int)并检查传递的整数是否在列表中。 You can then selectively disable list items after they've been clicked. 然后,您可以在单击列表项后有选择地禁用它们。

Adding some code: 添加一些代码:

public class MySimpleAdapter extends SimpleAdapter {

    private List<Integer> mDisabledRows;

    public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        mDisabledRows = new ArrayList<Integer>();
    }

    public void disableRow(int index) {
        mDisableRows.clear();
        mDisabledRows.add(index);
    }

    @Override
    public boolean isEnabled(int index) {
        return !mDisabledRows.contains(index);
    }
}

From your onItemClick method you would call adapter.disableRow(position) onItemClick方法中,您将调用adapter.disableRow(position)

I read that as you want to disable a row once it's clicked. 我读到这是因为您要在单击某行后禁用它。 If you only want to disable the last clicked row you could just store the last index clicked. 如果只想禁用最后单击的行,则可以存储最后单击的索引。

I have used below code and solved my problem. 我已经使用下面的代码并解决了我的问题。 It is very simple and easy. 这是非常简单和容易的。 No need to use any extra codes. 无需使用任何其他代码。
What I have done is I have put condition to check position and then based on it, I disabled Clicked Item. 我要做的是先检查条件,然后基于此条件禁用“单击的项目”。

My Code : 我的代码:

    int pos;
    SimpleAdapter adapter = new SimpleAdapter(this, arrlist, R.layout.topicwisequestion, new String[] { "option" }, new int[] { R.id.option }) {

                public boolean isEnabled(int position) {
                    if (position != 0) {
                        if (position == pos) {
                            return false;
                        } else {
                            return true;
                        }
                    } else {
                        return true;
                    }
                }
            };

            lvTWOptions.setAdapter(adapter);

            lvTWOptions.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                    pos = position;
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM