我是一名android初學者。 我正在做一個Android項目,它有一個函數來重新排列列表中的東西。 我發現了一個開源的https://github.com/commonsguy/cwac-touchlist#readme 和模塊名稱是CWAC:TouchListViewAndroid拖放ListView問題
但我的執行過程中,我有一些問題,希望有人能幫助我,
我想將關斷刪除函數當我在水平移動列表項,但我不能... 如果我評論在TouchListView.onTouchEvent()的情況下移除代碼MotionEvent.ACTION_CANCEL 它會發生意想不到的行爲。
另外,我想在拖動海豚瀏覽器書籤頁這樣的項目時有一些動畫,但我不知道它應該實現DragListener嗎?
但是,我修正了一個錯誤。
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (mGestureDetector != null) {
mGestureDetector.onTouchEvent(ev);
}
if ((mDragListener != null || mDropListener != null) && mDragView != null) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
Rect r = mTempRect;
mDragView.getDrawingRect(r);
stopDragging();
if (mRemoveMode == SLIDE_RIGHT && ev.getX() > r.left + (r.width() * 3/4)) {
if (mRemoveListener != null) {
mRemoveListener.remove(mFirstDragPos);
}
unExpandViews(true);
} else if (mRemoveMode == SLIDE_LEFT && ev.getX() < r.left + (r.width()/4)) {
if (mRemoveListener != null) {
mRemoveListener.remove(mFirstDragPos);
}
unExpandViews(true);
} else {
if (mDropListener != null && mDragPos >= 0 && mDragPos < getCount() - 1) {
mDropListener.drop(mFirstDragPos, mDragPos);
}
unExpandViews(false);
}
break;
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
int x = (int) ev.getX();
int y = (int) ev.getY();
dragView(x, y);
int itemnum = getItemForPosition(y);
if (itemnum >= 0) {
if (action == MotionEvent.ACTION_DOWN || itemnum != mDragPos) {
if (mDragListener != null) {
mDragListener.drag(mDragPos, itemnum);
}
mDragPos = itemnum;
doExpansion();
}
int speed = 0;
adjustScrollBounds(y);
if (y > mLowerBound) {
// scroll the list up a bit
speed = y > (mHeight + mLowerBound)/2 ? 16 : 4;
} else if (y < mUpperBound) {
// scroll the list down a bit
speed = y < mUpperBound/2 ? -16 : -4;
}
if (speed != 0) {
int ref = pointToPosition(0, mHeight/2);
if (ref == AdapterView.INVALID_POSITION) {
// we hit a divider or an invisible view, check
// somewhere else
ref = pointToPosition(0, mHeight/2 + getDividerHeight() + 64);
}
View v = getChildAt(ref - getFirstVisiblePosition());
if (v != null) {
int pos = v.getTop();
setSelectionFromTop(ref, pos - speed);
}
}
}
break;
}
return true;
}
return super.onTouchEvent(ev);
}
對於MotionEvent.ACTION_CANCEL,如果我拖動項目在list.getCount,它會拋出異常的情況,所以我更換條件從
if (mDropListener != null && mDragPos >= 0 && mDragPos < getCount()) {
mDropListener.drop(mFirstDragPos, mDragPos);
}
到
if (mDropListener != null && mDragPos >= 0 && mDragPos < getCount() - 1) {
mDropListener.drop(mFirstDragPos, mDragPos);
}
然後,異常將被修復。
任何人都可以幫助我? 非常感謝。
哦,謝謝。 我現在可以關閉移除功能。 我會嘗試實現動畫。 如果我可以實現此功能,我可以發佈解決方案。 – Rebecca