简体   繁体   中英

how to make an Activity scrollable even if no ScrollView is needed?

I have an Activity that includes some RelativeLayout s arranged vertically in a LinearLayout . now I want the user to be able to play with the activity by scrolling it up and down, I mean suppose I have 4 RelativeLayout s in a LinearLayout in my activity and they only occupy half of the screen vertically, so there is no need for ScrollView , but I want to scroll up/down (if the user scrolled up/down) and come back to first position (like an spring!). it's not a new thing and I'm sure you all know what I say! how to do that?

Add an ontouchlistener to your scrollview that wraps your outer linear layout and have the listener handle over scroll. You will have to modify a lot of this to meet your needs but it will provide the basics of how this would work. Or you could go with the fade edge property already in listview and save a lot of time not duplicating the Apple UI.

Example Touch Listener

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);
    }

}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM