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());