5

这里是显示列表视图项目和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; 
    } 
} 

但我不知道如何在GestureDetector实现所选择的项目像ItemClickListener并开始根据所选择的列表项其他活动。

任何人都请帮助我。

回答

6

使用ListView的pointToPosition方法在onDoubleTap方法:

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