双击listview项目时获取所选项目

时间:2011-07-15 18:51:04

标签: android android-listview double-click ontouchlistener gesturedetector

以下是显示listview项目和onclick侦听器操作的代码。

ListView list = (ListView) findViewById(R.id.list);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.list,
                android.R.layout.simple_list_item_1);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> l, View v, int position,
                    long id) {
                String sel = (String) adapterView
                            .getItemAtPosition(position);
                Toast.makeText(MyExample.this, "Your selection: " + sel, Toast.LENGTH_SHORT).show();
                if (sel.equals("Photos"){
                    startActivity(new Intent(MyExample.this, Photos.class));
                }   
            }

        });

现在,我需要实现只在双击时选择列表项。我尝试使用GestureDetector如下:

GestureDetector gestureDectector = new GestureDetector(this, new GestureListener());        
list.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                gestureDectector.onTouchEvent(event);
                return true;
            }
        });



public class GestureListener extends GestureDetector.SimpleOnGestureListener {

    public boolean onDown(MotionEvent e) {
        return true;
    }

    public boolean onDoubleTap(MotionEvent e) {
        Log.d("Double_Tap", "Yes, Clicked");
        return true;
    }
}

但我不知道如何在ItemClickListener中获取GestureDetector实现中的选定项目,并根据所选列表项启动另一个活动。

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:6)

使用pointToPosition方法中列表视图的onDoubleTap方法:

int position = list.pointToPosition(e.getX(), e.getY());