2015-10-14 22 views
1

我實現ExpandableListView DragNDrop使用此源https://github.com/sreekumarsh/android/tree/master/Drag%20N%20Drop子項。所有的主要功能是在課堂上,它擴展ExpandableListView。它覆蓋onTouchEvent(MotionEvent事件)方法,並且我指示的長按功能如下:並不總是工作在ExpandableListView子項長按

int flatPosition = pointToPosition(x, y); 
    dragRatio = getHeight()/screenHeight; 
    long packagedPosition = getExpandableListPosition(flatPosition); 

    Runnable mLongPressed = new Runnable() { 
     public void run() { 
      event.setLocation(x, y); 
      touchHandler(event); 
      pressedItem = true; 
     } 
    }; 

    if (action == MotionEvent.ACTION_DOWN 
      && getPackedPositionType(packagedPosition) == 1) { 
     if (dragOnLongPress) { 
      if (pressedItem) { 
       mDragMode = true; 
       pressedItem = false; 
      } else { 
       pressedItem = true; 
       **handler.postDelayed(mLongPressed, 500);** 
       return true; 
      } 
     } else if (x < dragOffset) { 
      mDragMode = true; 
     } 
    } 

但是,有一些問題。長時間點擊並不總是。我怎樣才能做到這一點更可靠的(不使用GestureDetector類)。

+0

你是不是總是工作是什麼意思?你期望的行爲是什麼? – DawidPi

+0

當檢測到長按,我設置的標誌,和未來在行動的依賴性,我選擇或移動子項。如果可以,在我的問題打開鏈接,發現類DragNDropListView。它擁有touchHandler方法中的所有代碼。謝謝 –

回答

0

我會分享我的個人執行,這似乎很好地工作。

final Handler handler = new Handler(); 
final Runnable mLongPressed = new Runnable() { 
      public void run() { 
       vibrator.vibrate(100); 


       Log.i("imageclick", "LONG_CLICK"); 
      } 
     }; 

whateverViewYouWantToDetectLongClick.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       Animation a = null; 
       switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
        Log.i("imageclick", "ACTION_DOWN"); 
        handler.postDelayed(mLongPressed, 750); 
        break; 

       case MotionEvent.ACTION_UP: 

        Log.i("imageclick", "ACTION_UP"); 
        handler.removeCallbacks(mLongPressed); 
        break; 
       case MotionEvent.ACTION_CANCEL: 
        Log.i("imageclick", "ACTION_CANCEL"); 
        handler.removeCallbacks(mLongPressed); 
        break; 
       default: 
        break; 
       } 


       return true; 
      } 
     });