Android 拖放ListView问题
我是一名安卓初学者。 我正在做一个 android 项目,它有一个功能可以对列表中的某些内容重新排序。 我在 https://github.com/commonsguy/cwac-touchlist#readme 模块名称是 CWAC: TouchListView
但是在我的实现过程中,我遇到了一些问题,希望有人可以帮助我,
我想在水平移动列表项时关闭删除功能,但我不能... 如果我在 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);
}
然后异常将被修复。
有人可以帮助我吗? 非常感谢。
I am a android beginner.
I am doing a android project which has a function to reorder something in a list.
I found a open source at https://github.com/commonsguy/cwac-touchlist#readme
and the module name is CWAC: TouchListView
But during my implementation, I have some problems and hope someone can help me,
I wanna to turnoff the remove function when I move the list item at horizontal but I cannot...
If I comment the remove code at TouchListView.onTouchEvent()'s case MotionEvent.ACTION_CANCEL
It will occur the unexpected behavior.Also, I wanna have something animation during dragging the item like dolphin browser bookmark page, but I don't know is it should be implements the DragListener??
However, I have a bug fixed on it.
@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);
}
For the case MotionEvent.ACTION_CANCEL, if I drag the item over the list.getCount, it will throw exception, so I replace the condition
from
if (mDropListener != null && mDragPos >= 0 && mDragPos < getCount() ) {
mDropListener.drop(mFirstDragPos, mDragPos);
}
to
if (mDropListener != null && mDragPos >= 0 && mDragPos < getCount() - 1) {
mDropListener.drop(mFirstDragPos, mDragPos);
}
Then the exception will be fixed.
Could anyone can help me??
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用值为
"none"
的自定义remove_mode
属性。例如:,抱歉,我无法帮助您。
Use the custom
remove_mode
attribute with a value of"none"
. For example:I will not be able to help you with that, sorry.