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

Rom*_*mah 5 android double-click ontouchlistener android-listview 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));
                }   
            }

        });
Run Code Online (Sandbox Code Playgroud)

现在,我需要实现只在双击时选择列表项.我尝试使用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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

有人请帮助我.

Rid*_*lly 6

pointToPosition在您的方法中使用listview的onDoubleTap方法:

int position = list.pointToPosition(e.getX(), e.getY());
Run Code Online (Sandbox Code Playgroud)