我使用此源https://github.com/sreekumarsh/android/tree/master/Drag%20N%20Drop为子项实现ExpandableListView DragNDrop。所有主要功能都在类中,它扩展了ExpandableListView。它覆盖了onTouchEvent(MotionEvent事件)方法,以及我指示的长按功能:
@Override
public boolean onTouchEvent(MotionEvent event) {
return touchHandler(event);
}
public boolean touchHandler(final MotionEvent event) {
final int action = event.getAction();
final int x = (int) event.getX();
final int y = (int) event.getY();
if (prevY < 0) {
prevY = y;
}
int flatPosition = pointToPosition(x, y);
dragRatio = getHeight() / screenHeight;
long packagedPosition = getExpandableListPosition(flatPosition);
Log.i(TAG, "Motion event " + event.getAction());
Runnable mLongPressed = new Runnable() {
public void run() {
Log.i("in thread", "ok");
event.setLocation(x, y);
touchHandler(event);
pressedItem = true;
isLongPressed = false;
}
};
if (action == MotionEvent.ACTION_DOWN && getPackedPositionType(packagedPosition) == 1) {
if (dragOnLongPress) {
Log.i("pressedItem", pressedItem + "");
if (pressedItem) {
mDragMode = true;
pressedItem = false;
Vibrator vibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(50);
} else {
pressedItem = true;
Log.i("thread: ", "starting");
handler.postDelayed(mLongPressed, 500);
return true;
}
} else if (x < dragOffset) {
mDragMode = true;
}
}
if (!mDragMode) {
/** when user action on other areas */
if ((pressedItem && Math.abs(prevY - y) > 30)
|| event.getAction() != MotionEvent.ACTION_MOVE) {
pressedItem = false;
handler.removeCallbacksAndMessages(null);
}
return super.onTouchEvent(event);
}
switch(action){
case:MotionEvent.ACTION_DOWN: bla-bla
case:MotionEvent.ACTION_MOVE: bla-bla
case:MotionEvent.ACTION_UP: bla-bla
}
当正常工作时,它会像这样记录:
I/Motion event MotionEvent.ACTION_DOWN:
I/pressedItem: false
I/thread: starting
I/in thread: ok
I/Motion event MotionEvent.ACTION_DOWN:
I/pressedItem: true
I/Motion event MotionEvent.ACTION_MOVE
I/Motion event MotionEvent.ACTION_UP
但是当没有检测到长按时:
I/Motion event MotionEvent.ACTION_DOWN:
I/pressedItem: false
I/Motion event MotionEvent.ACTION_MOVE
I/Motion event MotionEvent.ACTION_MOVE
I/Motion event MotionEvent.ACTION_MOVE
I/Motion event MotionEvent.ACTION_MOVE
...
I/Motion event MotionEvent.ACTION_MOVE
I/Motion event MotionEvent.ACTION_UP
我的dragmod没有转。
在这两种情况下,子项目按下都是相同的,但它可以工作不同。 MotionEvent.ACTION_MOVE显示为Presed。它可以是自定义的roms或htc意义上。在库存ROM它几乎总是很好。我该怎么办呢?如果您还有其他问题,请告诉我。