检测是否在适配器中的getView()内部检查了项目

时间:2014-05-02 17:43:07

标签: android listview adapter

按下listView行时,我执行此操作:listView.setItemChecked(position, value)。这会使用selector更改背景颜色,以显示位置处于活动状态。

我也在adapter的{​​{1}}方法中执行基本的斑马条纹。像这样:

getView()

现在在这个if (pos % 2 == 1) { convertView.setBackgroundColor(Color.parseColor("#99DEEAF1")); } else if (viewType != 0) { convertView.setBackgroundColor(Color.parseColor("#00000000")); } 方法中,我想知道我的项目(在当前getView()中)是否已经过检查,以便我不会尝试更改背景颜色(如上所示) )并覆盖我position的选择器。这可能吗?

最后,我想在listView语句中添加一个子句,如果选中了该项,则不会执行任何一个。

2 个答案:

答案 0 :(得分:1)

您可以使用这样的辅助类来记住选定的位置:

public class ListChoice
{
    private int selection;

    public void setListChoice(int s)
    {
        selection = s;
    }

    public int getListChoice()
    {
        return selection;
    }
}

然后:

listView.setItemChecked(position, value);
ListChoice lc = new ListChoice();
lc.setListChoice(position);

并在getView()

if(pos != lc.getListChoice())
{
    if (pos % 2 == 1) 
    {
        convertView.setBackgroundColor(Color.parseColor("#99DEEAF1"));
    } 
    else if (viewType != 0) 
    {
        convertView.setBackgroundColor(Color.parseColor("#00000000"));
    }
}

答案 1 :(得分:0)

您可以创建一个Model类,注意是否在getView()方法中选中了列表项。

OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            //Update the model here
        }
    };

如需帮助,请查看此link