Android的 - 使用的ListView和onItemClick onTouch [英] Android - ListView using onItemClick and onTouch
问题描述
我尝试实现的东西就像一个拖放的一个ListView。
任务是简单的。我有一个侧菜单中的在这里我把一些对象在列表中。他们都表示没有。我要的是拖放他们中的一个在屏幕上。
I try to implement something like a "drag and drop" for a ListView. The task is simple. I have a "side menu" where I put some objects in a list. They are all shown there. What I want is to drag and drop one of them to the screen.
我第一次尝试使用 OnTouchListener 这为我提供了拖放放大器;拖放功能我所追求的。所以我做这样的事情。
I first tried to use the OnTouchListener which offers me the drag&drop functionality I seek. So I do something like
@Override
public boolean onTouch(View view, MotionEvent movEv) {
if (movEv.getAction() == MotionEvent.ACTION_DOWN)
// DRAG
else if (movEv.getAction() == MotionEvent.ACTION_UP)
// DROP
return false;
}
问题的关键是,这只是给我的MotionEvent像X和Y位置的信息。
我需要的是一个被点击的项目就知道了。像在 OnItemClickListener
问题是,如果我在项目上点击,这意味着我不动的手指OnItemClickListener才起作用。我移动和释放它的那一刻,监听器不承认这是一个点击了。因此,使用
The problem is, the OnItemClickListener only works if I "click" on an item, implying I don't move the finger. The moment I move and release it, the Listener doesn't recognize this as a click any more. Therefore using
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// I know which item was pressed -> position
}
不起作用。
有没有人有任何想法如何解决这个问题?我能以某种方式使用onItemClick已经在关于pressed的时刻?
Does anyone have any idea how to solve this? Can I somehow use the onItemClick already at the "onPressed" moment?
感谢您的帮助!
推荐答案
相反OnItemClickListener,你可以使用<一个href=\"http://developer.android.com/reference/android/widget/AbsListView.html#pointToPosition(int,%20int)\"相对=nofollow>在OnTouchListener pointToPosition 方法。它可以是这样的:
Instead of OnItemClickListener you can use pointToPosition method in your OnTouchListener. It can be something like this:
@Override
public boolean onTouch(View view, MotionEvent movEv) {
if (movEv.getAction() == MotionEvent.ACTION_DOWN) {
int itemPosition = listView.pointToPositon(movEv.getX(), movEv.getY());
// DRAG
}
else if (movEv.getAction() == MotionEvent.ACTION_UP)
// DROP
return false;
}
这篇关于Android的 - 使用的ListView和onItemClick onTouch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!