首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android Gallery翻转问题

Android Gallery翻转问题
EN

Stack Overflow用户
提问于 2012-01-28 01:33:42
回答 2查看 437关注 0票数 0

我有一个包含TextView标签和下面列表视图的视图图库。它工作得很好,只是为了让它从一个元素翻到另一个元素,用户必须触摸列表视图上方(标签附近),并快速移动或在图库对象之间进行触摸。有时,在listview下的作品too.But,我真的希望能够在触摸listview的同时也能尽情挥舞,因为它占据了屏幕的大部分。如何做到这一点?您需要查看哪些代码?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-24 08:44:33

我遇到了类似的问题,并通过重写图库并实现onInterceptTouchEvent来解决这个问题,以确保移动事件被图库截获,所有其他事件都得到正常处理。

在onInterceptTouchEvent中返回true会导致该触摸序列中的所有后续触摸事件都被发送到此视图,如果返回false,则将事件留给其子对象。TouchSlop是必需的,因为当进行单击时,有时会有少量的移动。

我很乐意声称这是我自己的想法,但从默认的Android Launcher代码中获得了代码的基础。

代码语言:javascript
复制
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;
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2012-01-28 02:44:45

您可能希望在列表视图上设置onTouchListener(),或者设置整个线性/相对布局。

getListView().setOnTouchListener(yourlistener)或在整个布局上设置它。如果你发布一点代码,我可以进一步帮助你。XML以及如何在Java类中使用将是最有帮助的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9037512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档