2014-01-28 55 views
0

我試圖在觸摸列表視圖時獲取位置,以便在特定位置觸摸時顯示敬酒。如何在列表視圖中獲取觸摸位置?

這裏是我的觸動聽者的列表視圖: -

customListFilter.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View arg0, MotionEvent event) { 
      int actionX = (int) event.getX(); 
      int actionY = (int) event.getY(); 
      int extraTapArea = 13; 
      int x = (int) (actionX + extraTapArea); 
      int y = (int) (actionY - extraTapArea); 
      x = getWidth() - x; 

      if(x <= 0){ 
       x += extraTapArea; 
      } 
      if (y <= 0) 
       y = actionY;     

      if ((actionX,actionY).contains(x, y)) { 
       // Show Toast Here 
      } 
      return false; 
     } 
    }); 

請幫我看看我的邏輯。 基本上,如果我碰在ListView右側或左側,但沒有中間環節,它應該顯示舉杯

+2

如果您贊成原因,請做評論。 –

回答

1

嘗試類似如下:

Display display = getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 
display.getSize(size); 
int width = size.x; 
customListFilter.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View arg0, MotionEvent event) { 
      int actionX = (int) event.getX(); 
      int actionY = (int) event.getY(); 

     int middlePoint = width/2 ; 

     if(actionX >middlePoint){ 
      // you are touching the right side 
     }else if (actionX <middlePoint){ 
      // you are touching the left side 
     } 

         return false; 
     } 
    }); 

,給我一些反饋

希望有所幫助。

+0

Awesome Answer。謝謝。告訴我,這是我的問題,得到downvoted不好? –

+0

它不是我誰downvote,所以我不知道:) ,歡迎你:) –