我有一个Activity
包含一些RelativeLayout
垂直排列在 a 中的 s LinearLayout
。现在我希望用户能够通过上下滚动来玩活动,我的意思是假设我的活动RelativeLayout
中有 4 秒LinearLayout
,它们只垂直占据屏幕的一半,所以不需要ScrollView
, 但是我想向上/向下滚动(如果用户向上/向下滚动)并返回到第一个位置(就像弹簧一样!)。这不是什么新鲜事,我相信你们都知道我在说什么!怎么做?
问问题
410 次
1 回答
2
将 ontouchlistener 添加到您的滚动视图中,它包裹您的外部线性布局并让侦听器处理滚动。您将不得不修改很多内容以满足您的需求,但它将提供如何工作的基础知识。或者您可以使用列表视图中已有的淡入淡出边缘属性,并节省大量时间而不是复制 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);
}
}
}
于 2013-09-18T20:23:31.523 回答