Listview保持并按下onTouchListener

时间:2014-03-24 17:56:54

标签: android listview android-listview ontouchlistener

所以,我已经看到很多人在询问如何处理列表视图上的滑动但是我想知道如何在按下该特定项目的listview项目时触发和AlertDialog的示例。因此,当我按下并按住列表视图上的特定项目时,onTouch方法应该识别它是哪个项目。

l.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                int action = event.getAction();
                AlertDialog.Builder alert = new AlertDialog.Builder(ctx);
                alert.setTitle("Delete Friend?");
                alert.setPositiveButton("Delete", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface d, int which) {
                        //onTouchListener should recognize which friend is being pressed on,
                        //similar to onItemClickListener

                    }
                });

                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        //Do Nothing
                    }
                });
                if(action == MotionEvent.ACTION_DOWN){
                    alert.create().show();
                }

                return false;
            }
        });

1 个答案:

答案 0 :(得分:1)

您可以使用长按监听器。

例如

    listView.setOnItemLongClickListener(new AdapaterView.OnItemLongClickListener(){
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id){
        //Add alertDialog here
        }
    });