Android Eclipse Horizo​​ntalScrollView Fling / Swipe帮助

时间:2011-08-08 17:32:14

标签: android android-layout

我的Android应用中有一些自定义视图。我想将它们添加到Horizo​​ntalScrollView中,并能够通过它们进行推送/滑动。

有人可以帮我吗?

感谢。

1 个答案:

答案 0 :(得分:0)

在您的代码中尝试此操作。您必须实现OnGestureListener和OnClickListener:

private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;

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

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) //
{
    int changeInX = (int) (e2.getX() - e1.getX());

    // If the swipe was big enough, do something
    if (Math.abs(changeInX) > SWIPE_MIN_DISTANCE
            && Math.abs(velocityX) > SWIPE_MIN_VELOCITY) //
    {
        if (velocityX > 0) //
            activity.animateSwipe(true);
        else
            activity.animateSwipe(false);

        return true;
    }
    else
        return false;
}

    public void animateSwipe(boolean right) {
    TranslateAnimation anim = null;

    LinearLayout layout = (LinearLayout) this
            .findViewById(R.id.layoutVolume);

    if (right) {
        anim = new TranslateAnimation(0.0f, layout.getWidth(), 0.0f, 0.0f);
    } else {
        anim = new TranslateAnimation(0.0f, -layout.getWidth(), 0.0f, 0.0f);
    }

    anim.setDuration(250);
    anim.setInterpolator(new AccelerateInterpolator(1.0f));

    layout.startAnimation(anim);
}