How to disable ListView item after it has been clicked?

北慕城南 提交于 2019-12-12 13:29:03

问题


I have a simple array of Strings that I was displaying in a horizontal ListView with an ArrayAdapter. What I'm looking to do is: when the user selects an item from the ListView, make that item not clickable and change the background color of that item. Perhaps like a "grayed-out" look to it. I was looking into creating a custom Adapter and overriding the isEnabled(int position) method but I don't know how I would go about this. Any advice, suggestions, or help will be greatly appreciated thanks!


回答1:


I was looking into creating a custom Adapter and overriding the isEnabled(int position) method but I don't know how I would go about this.

This is quite easy to do. I recommend a SparseBooleanArray to track the enabled items for efficiency:

public class MyAdapter extends ArrayAdapter<String> {
    private SparseBooleanArray enabledItems = new SparseBooleanArray(); 

    public MyAdapter(Context context, int textViewResourceId, List<String> objects) {
        super(context, textViewResourceId, objects);
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return enabledItems.get(position, true);
    }

    public void toggleItem(int position) {
        boolean state = enabledItems.get(position, true);
        enabledItems.put(position, !state);
    }
}

The AutoComplete feature of Eclipse did must of the work, but here are some quick notes:

  • You must override areAllItemsEnabled() along with isEnabled()
  • I designed toggle() to be used by an onItemClickListener() you only need to call adapter.toggle(position)
  • If you want to change the row's appearance (more than what enabling and disabling does by default) simply override getView(). Don't forget to cover both cases:

    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = super.getView(position, convertView, parent);
    
        if(!isEnabled(position)) { 
            /* change to disabled appearance */ 
        } 
        else { 
            /* restore default appearance */ 
        }
        return convertView;
    }
    

    Hope that helps!




回答2:


pass position to adapter class when you click on list item

 @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        adapter.setSelectedIndex(position);
    }

add method of setSelectedIndex to adapter class

  public void setSelectedIndex(int ind)
    {
        selectedIndex = ind;
        notifyDataSetChanged();
    }

Now check the postion of this listview if same then enable and disable value in getView me method

 if(selectedIndex!= -1 && position == selectedIndex)
        {
            holder.tv.setBackgroundColor(Color.BLACK);
        }
        else
        {
            holder.tv.setBackgroundColor(selectedColor);
        }
        holder.tv.setText("" + (position + 1) + " " + testList.get(position).getTestText());

Reference from here




回答3:


Use setEnabled(bool) property:

yourlistview.setEnabled(false);



回答4:


Not sure whether it will work or not

    public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
                // your code
                view.setBackgroundColor(Color.BLUE);
                view.setEnabled(false);
    }


来源:https://stackoverflow.com/questions/15885928/how-to-disable-listview-item-after-it-has-been-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!