即使不需要ScrollView,如何使Activity可滚动?

时间:2013-09-18 19:46:39

标签: java android android-activity scrollview

我有一个Activity,其中包含RelativeLayout中垂直排列的LinearLayoutRelativeLayout。现在我希望用户能够通过上下滚动来播放活动,我的意思是假设我的活动LinearLayout中有4个ScrollView,他们只占用了屏幕的一半垂直,所以不需要{{1}},但我想向上/向下滚动(如果用户向上/向下滚动)并回到第一个位置(就像一个弹簧!)。这不是新事物,我相信你们都知道我说的话!怎么做?

1 个答案:

答案 0 :(得分:2)

在滚动视图中添加一个ontouchlistener,它包裹你的外部线性布局并让侦听器处理滚动。您将不得不修改其中许多内容以满足您的需求,但它将提供如何工作的基础知识。或者您可以使用listview中已有的淡化边缘属性,并节省大量时间而不会复制Apple UI。

示例触摸侦听器

using System;
using System.Collections.Generic;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Views.Animations;


namespace SomeProgram
{

public class ViewFlipperSwipeDetector : ParentActivity, View.IOnTouchListener
{


    //private Activity activity;
    private ViewFlipper viewflipper;
    int MIN_DISTANCE = 100;
    private float downX, downY, upX, upY;


    public ViewFlipperSwipeDetector(ViewFlipper viewflipper)
    {
        //this.activity = activity;
        this.viewflipper = viewflipper;

    }

   private Animation inFromRightAnimation() 
   {
    Animation inFromRight = new TranslateAnimation(
    Dimension.RelativeToParent, +1.0f, Dimension.RelativeToParent, 0.0f,
    Dimension.RelativeToParent, 0.0f, Dimension.RelativeToParent, 0.0f);

    inFromRight.Duration = 400;
    inFromRight.Interpolator = new AccelerateInterpolator();
    return inFromRight;
    }

    private Animation outToLeftAnimation() 
    {
     Animation outtoLeft = new TranslateAnimation(
     Dimension.RelativeToParent, 0.0f, Dimension.RelativeToParent, -1.0f,
     Dimension.RelativeToParent, 0.0f, Dimension.RelativeToParent, 0.0f);

    outtoLeft.Duration = 400;
    outtoLeft.Interpolator = new AccelerateInterpolator();
    return outtoLeft;
    }

    private Animation inFromLeftAnimation() 
    {
    Animation inFromLeft = new TranslateAnimation(
    Dimension.RelativeToParent, -1.0f, Dimension.RelativeToParent, 0.0f,
    Dimension.RelativeToParent, 0.0f, Dimension.RelativeToParent, 0.0f);
    inFromLeft.Duration = 400;
    inFromLeft.Interpolator = new AccelerateInterpolator();
    return inFromLeft;
    }

    private Animation outToRightAnimation() 
    {
    Animation outtoRight = new TranslateAnimation(
    Dimension.RelativeToParent, 0.0f, Dimension.RelativeToParent, +1.0f,
     Dimension.RelativeToParent, 0.0f, Dimension.RelativeToParent, 0.0f);
    outtoRight.Duration = 400;
    outtoRight.Interpolator = new AccelerateInterpolator();
    return outtoRight;
    }

    protected void onRightToLeftSwipe()
    {
        //Toast.MakeText(activity, "RightToLeftSwipe!", ToastLength.Short).Show();
        viewflipper.InAnimation = inFromRightAnimation();
        viewflipper.OutAnimation = outToLeftAnimation();
        viewflipper.ShowNext();

    }

    protected void onLeftToRightSwipe()
    {

       // Toast.MakeText(activity, "LeftToRightSwipe!", ToastLength.Short).Show();
        viewflipper.InAnimation = inFromLeftAnimation();
        viewflipper.OutAnimation = outToRightAnimation();
        viewflipper.ShowPrevious();

    }

    protected void onTopToBottomSwipe()
    {
        //Toast.MakeText(activity, "TopToBottomSwipe!", ToastLength.Short).Show();
        //activity.doSomething();
    }

    protected void onBottomToTopSwipe()
    {
        //Toast.MakeText(activity, "BottomToTopSwipe!", ToastLength.Short).Show();
        //activity.doSomething();
    }


    public bool OnTouch(View v, MotionEvent e)
    {
        switch (e.Action)
        {
            case MotionEventActions.Down:
                {
                    downX = e.GetX();
                    downY = e.GetY();
                    return true;
                }
            case MotionEventActions.Up:
                {
                    upX = e.GetX();
                    upY = e.GetY();

                    float deltaX = downX - upX;
                    float deltaY = downY - upY;

                    // swipe horizontal?
                    if (Math.Abs(deltaX) > MIN_DISTANCE)
                    {
                        // left or right
                        if (deltaX < 0) { this.onLeftToRightSwipe(); return true; }
                        if (deltaX > 0) { this.onRightToLeftSwipe(); return true; }
                    }
                    else
                    {
                        //Toast.MakeText(activity, "Swipe was only " + Math.Abs(deltaX) + " long, need at least " + MIN_DISTANCE, ToastLength.Short).Show();
                        return false; 
                    }

                    // swipe vertical?
                    if (Math.Abs(deltaY) > MIN_DISTANCE)
                    {
                        // top or down
                        if (deltaY < 0) { this.onTopToBottomSwipe(); return true; }
                        if (deltaY > 0) { this.onBottomToTopSwipe(); return true; }
                    }
                    else
                    {
                        //Toast.MakeText(activity, "Swipe was only " + Math.Abs(deltaX) + " long, need at least " + MIN_DISTANCE, ToastLength.Short);
                        return false; 
                    }

                    return true;
                }
        }
        return false;
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
    }

}
}