检查项目是否与上次单击/列表视图相同

时间:2011-07-05 18:32:52

标签: java android listview

我想做的是“简单”,我点击列表视图中的某个项目,会发生一些事情。我再次点击一个项目,并根据它是否是我点击的相同项目或另一个项目,它会做其他事情。你能帮助我吗?

public OnItemClickListener listener = new OnItemClickListener() {
    int i = 1;
    int p;
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id){
        loaddetails(pf, position);
        i++;
        p=position;
        if(i%2 == 0 ){
            details.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.enter));
            details.setVisibility(View.VISIBLE);
        }else{
            details.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade2));
            details.setVisibility(View.GONE);
        }
    }
};

我的例子是这样的,我点击一个项目,细节出现,只有当我点击相同的项目时,细节必须消失。

1 个答案:

答案 0 :(得分:0)

保持点击最后一项的ID占位符:

全班同学:

int lastID = -1;

然后:

public OnItemClickListener listener = new OnItemClickListener() {
    int i = 1;
    int p;
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id){
        loaddetails(pf, position);
        i++;
        p=position;
        if(lastID == id || lastID == -1){
            details.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.enter));
            details.setVisibility(View.VISIBLE);
        }else{
            details.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade2));
            details.setVisibility(View.GONE);
        }
        lastID = id;
    }
};