同时使用setOnItemClickListener和setOnItemLongClickListener两个侦听器同时触发或同时工作。为什么

时间:2013-06-06 14:08:46

标签: android android-layout android-intent

ListView我正在使用setOnItemClickListener()setOnItemLongClickListener()。当我点击一个项目时它工作正常,但是当我长按一个项目时,有时两个听众同时被解雇或同时工作。为什么?

/**
* on click of list view item show the run time webview.
*/
mListViewStar.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
        isHome = true;
        Animation translate1 = AnimationUtils.loadAnimation(Home.this,
                R.anim.tran_right);
        mRelativeLayout.setVisibility(View.GONE);
        mRelativeLayout.startAnimation(translate1);
        // mProgressBar.setVisibility(View.VISIBLE);
        mProgressDialog = ProgressDialog.show(Home.this, "",
                "Loading...");
        addWebView(mArrayListJBSelectedUrls.get(arg2)
            .getStrSelectedWebsiteUrl().toString(), arg2);
    }
});
/**
* on long press delete item from list view
*/
mListViewStar.setOnItemLongClickListener(new OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
        position = arg2;
        AlertDialog.Builder mAlert = new AlertDialog.Builder(Home.this);
        mAlert.setTitle(getString(R.string.alert));
        mAlert.setIcon(R.drawable.logo);
        mAlert.setMessage(getString(R.string.delete_item_dialog));
        mAlert.setPositiveButton(getString(R.string.yes),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog,
                        int which) {
                    mDataBaseMethod.open();
                    Log.i("Delete", ""
                        + mArrayListJBSelectedUrls
                        .get(position)
                        .getStrSelectedUrlId());
                    mDataBaseMethod
                        .deleteWebSites(mArrayListJBSelectedUrls
                        .get(position)
                        .getStrSelectedUrlId()
                        .toString());
                    mArrayListJBSelectedUrls.remove(position);
                    starBaseAdapter.notifyDataSetChanged();
                    mDataBaseMethod.close();
                }
        });
        mAlert.setNegativeButton(getString(R.string.no),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog,
                        int which) {
                    dialog.dismiss();
                }
        });
        AlertDialog alertDialog = mAlert.create();
        alertDialog.show();
        return true;
    }
});

4 个答案:

答案 0 :(得分:27)

在onItemLongClick

上添加return true

答案 1 :(得分:1)

setOnClickListener首先激活,如果你想要两个动作一起启动问题,为了避免这种情况你需要返回true ; on OnItemLongClickListener。

lv_frag_three.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //Groups clickedObj = (Groups) adapterView.getItemAtPosition(i);
                // your actions
            }
        });
});

  lv_frag_three.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                        return true;
                    }
                });

答案 2 :(得分:-1)

尝试修复

 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3)

public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3)

public void onItemClick(AdapterView<?> arg0,final View arg1,final int arg2,
          final long arg3)

public boolean onItemLongClick(AdapterView<?> arg0,final View arg1,
               final int arg2,final long arg3)

答案 3 :(得分:-1)

您必须在函数false上返回onItemLongClick