我有一个包含TextView标签和下面列表视图的视图图库。它工作得很好,只是为了让它从一个元素翻到另一个元素,用户必须触摸列表视图上方(标签附近),并快速移动或在图库对象之间进行触摸。有时,在listview下的作品too.But,我真的希望能够在触摸listview的同时也能尽情挥舞,因为它占据了屏幕的大部分。如何做到这一点?您需要查看哪些代码?
发布于 2012-02-24 08:44:33
我遇到了类似的问题,并通过重写图库并实现onInterceptTouchEvent来解决这个问题,以确保移动事件被图库截获,所有其他事件都得到正常处理。
在onInterceptTouchEvent中返回true会导致该触摸序列中的所有后续触摸事件都被发送到此视图,如果返回false,则将事件留给其子对象。TouchSlop是必需的,因为当进行单击时,有时会有少量的移动。
我很乐意声称这是我自己的想法,但从默认的Android Launcher代码中获得了代码的基础。
public class MyGallery extends Gallery{
private MotionEvent downEvent;
private int touchSlop;
private float lastMotionY;
private float lastMotionX;
public MyGallery(Context context) {
super(context);
initTouchSlop();
}
private void initTouchSlop() {
final ViewConfiguration configuration = ViewConfiguration.get(getContext());
touchSlop = configuration.getScaledTouchSlop();
}
@Override public boolean onInterceptTouchEvent(MotionEvent ev) {
final float x = ev.getX();
final float y = ev.getY();
switch (ev.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_MOVE: {
final int xDiff = (int) Math.abs(x - lastMotionX);
final int yDiff = (int) Math.abs(y - lastMotionY);
// have we moved enough to consider this a scroll
if (xDiff > touchSlop || yDiff > touchSlop) {
// this is the event we want, but we need to resend the Down event as this could have been consumed by a child
Log.d(TAG, "Move event detected: Start intercepting touch events");
if (downEvent != null) this.onTouchEvent(downEvent);
downEvent = null;
return true;
}
return false;
}
case MotionEvent.ACTION_DOWN: {
// need to save the on down event incase this is going to be a scroll
downEvent = MotionEvent.obtain(ev);
lastMotionX = x;
lastMotionY = y;
return false;
}
default: {
// if this is not a down or scroll event then it is not for us
downEvent = null;
return false;
}
}
}发布于 2012-01-28 02:44:45
您可能希望在列表视图上设置onTouchListener(),或者设置整个线性/相对布局。
getListView().setOnTouchListener(yourlistener)或在整个布局上设置它。如果你发布一点代码,我可以进一步帮助你。XML以及如何在Java类中使用将是最有帮助的。
https://stackoverflow.com/questions/9037512
复制相似问题