Android、Tabhost 中的 ViewFlipper 和 GestureDetector

发布于 2024-12-20 11:36:33 字数 225 浏览 2 评论 0原文

我想制作一个 tabhost,并且想为一项活动添加一个 viewflipper。 此活动适用于 tabhost。 我已经写了,但什么不起作用。有人知道我该怎么做吗?我希望他们明白我想要什么。

在此处输入图像描述

我想在此活动中使用 GestureDetector。我可以用按钮更改图片,但用手指却不能。

I wanted to make one tabhost and I wanted to add one viewflipper one activity.
This activity works with tabhost.
I've written but what does not work. Have anybody an idea how can I do that? I hope they understand what I wanted.

enter image description here

I wanted to use in this activitiy of the GestureDetector. I can change the pictures with button but with finger i cant.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山田美奈子 2024-12-27 11:36:33

我以前做过。 ( https://github.com/vancexu/AimTo/tree/ master/src/com/hackingtrace/vancexu AimToActivity.java)
只需实现您自己的 GestureDetector ,然后在您的选项卡活动中注册一个 OnTouchListener

顺便说一句,我还实现了幻灯片动画,使用 ViewFlipper 中的 < code>TabHost

我的 TabHostActivity 中的一些代码

private static final int SWIPE_MIN_DISTANCE = 180; 
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;

public void onCreate(Bundle savedInstanceState) {
    ....
    gestureDetector = new GestureDetector(new MyGestureDetector());

    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }
    };

class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        TabHost tabHost = getTabHost();
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            // right to left swipe, tab change to right
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Log.i("Gesture ", "right");
                if (currentView == maxTabIndex) {
                    currentView = 0;
                } else {
                    currentView++;
                }
                viewFlipperBody.setInAnimation(slideLeftIn);
                viewFlipperBody.setOutAnimation(slideLeftOut);
                viewFlipperBody.setDisplayedChild(currentView);

            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Log.i("Gesture ", "left");
                if (currentView == 0) {
                    currentView = maxTabIndex;
                } else {
                    currentView--;
                }
                viewFlipperBody.setInAnimation(slideRightIn);
                viewFlipperBody.setOutAnimation(slideRightOut);
                viewFlipperBody.setDisplayedChild(currentView);
            }
        } catch (Exception e) {
            // nothing
            e.printStackTrace();
        }
        tabHost.setCurrentTab(currentView);
        return false;
    }
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {

    if (gestureDetector.onTouchEvent(event)) {
        event.setAction(MotionEvent.ACTION_CANCEL);
    }
    return super.dispatchTouchEvent(event);
}

I have made it before. ( https://github.com/vancexu/AimTo/tree/master/src/com/hackingtrace/vancexu AimToActivity.java)
Just implement your own GestureDetector ,then in your tab activity register a OnTouchListener

By the way, I also implement the slide animation, using ViewFlipper in TabHost

Some code in my TabHostActivity

private static final int SWIPE_MIN_DISTANCE = 180; 
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;

public void onCreate(Bundle savedInstanceState) {
    ....
    gestureDetector = new GestureDetector(new MyGestureDetector());

    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }
    };

class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        TabHost tabHost = getTabHost();
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            // right to left swipe, tab change to right
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Log.i("Gesture ", "right");
                if (currentView == maxTabIndex) {
                    currentView = 0;
                } else {
                    currentView++;
                }
                viewFlipperBody.setInAnimation(slideLeftIn);
                viewFlipperBody.setOutAnimation(slideLeftOut);
                viewFlipperBody.setDisplayedChild(currentView);

            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Log.i("Gesture ", "left");
                if (currentView == 0) {
                    currentView = maxTabIndex;
                } else {
                    currentView--;
                }
                viewFlipperBody.setInAnimation(slideRightIn);
                viewFlipperBody.setOutAnimation(slideRightOut);
                viewFlipperBody.setDisplayedChild(currentView);
            }
        } catch (Exception e) {
            // nothing
            e.printStackTrace();
        }
        tabHost.setCurrentTab(currentView);
        return false;
    }
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {

    if (gestureDetector.onTouchEvent(event)) {
        event.setAction(MotionEvent.ACTION_CANCEL);
    }
    return super.dispatchTouchEvent(event);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文