在android中实现滑动和点击相对布局的最佳方法是什么?

sou*_*nda 5 c# android xamarin

在此处输入图片说明
我正在创建一个相对布局并希望添加点击和滑动(触摸并在布局上移动指针以实时移动它)并检测左右滑动。到目前为止,我已经尝试了以下代码。

public bool OnTouch(View v, MotionEvent e)
    {
        if (gestureDetector.OnTouchEvent(e))
        {
            //This is a Click
            return true;
        }
        else
        {
            int initialTouchX = 0, initialTouchY = 0;
            int newx = 0;
            var x = v.Left;
            switch (e.Action)
            {

                case MotionEventActions.Down:
                    {
                        _viewX = e.GetX();
                        _viewY = e.GetY();
                        initialTouchX = (int)e.RawX;
                        initialTouchY = (int)e.RawY;
                        break;
                    }

                case MotionEventActions.Move:
                    {

                        var left = (int)(e.RawX - _viewX);
                        newx = left;
                        var right = (int)(left + v.Width);

                        var top = (int)(e.RawY - _viewY);
                        var bottom = (int)(top + v.Height);

                        v.Layout(left, top, right, bottom);

                        break;
                    }
                case MotionEventActions.Up:
                    {
                        int lastX = (int)e.GetX();
                        int lastY = (int)e.GetY();
                       if ((x - newx) > 40)
                        {
                          //Detect Right Swipe

                        }
                        else if ((newx - x > 40))
                        {
            //Detect Left Swipe
                        }
                        else
                        {
                            //Skip others
                        }
        break;
                    }

            }
        }
        return true;
    }
Run Code Online (Sandbox Code Playgroud)

我的gestureDetector.OnTouchEvent(e) 代码

gestureDetector = new GestureDetector(this, new SingleTapUp());

class SingleTapUp : Android.Views.GestureDetector.SimpleOnGestureListener 
    {

        public override bool OnSingleTapUp(MotionEvent e) {
           // Toast.MakeText(this,, ToastLength.Long).Show();
            return true;
        }

    }
Run Code Online (Sandbox Code Playgroud)

我的代码在某些设备和模拟器上运行良好。但有时不工作,并且onclick布局会自动移动(布局以触摸指针为中心)。我认为有些事情是错误的并导致了问题。你可以建议我最好/标准的方法来做到这一点。任何帮助将不胜感激。

CDr*_*sos 3

我以这种方式实现 swype:

public class MainActivity : Activity, GestureDetector.IOnGestureListener
{
private GestureDetector _gestureDetector;

public bool OnDown(MotionEvent e)
    {
        return false;
    }
    public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        bool result = false;
        int SWIPE_THRESHOLD = 80;
        int SWIPE_VELOCITY_THRESHOLD = 80;
        try
        {
            float diffY = e2.GetY() - e1.GetY();
            float diffX = e2.GetX() - e1.GetX();
            if (Math.Abs(diffX) > Math.Abs(diffY))
            {
                if (Math.Abs(diffX) > SWIPE_THRESHOLD && Math.Abs(velocityX) > SWIPE_VELOCITY_THRESHOLD)
                {
                    if (diffX > 0)
                    {
                        //code for swipe right here

                    }
                    else
                    {
                        //code for swipe Left here

                    }
                }
            }
        }
        catch (Exception exception)
        {
            //exception.printStackTrace();
        }
        return result;
    }
    public void OnLongPress(MotionEvent e) {}
    public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
    {
        return false;
    }
    public void OnShowPress(MotionEvent e) {}
    public bool OnSingleTapUp(MotionEvent e)
    {
        return false;
    }
public override bool OnTouchEvent(MotionEvent e)
    {
        _gestureDetector.OnTouchEvent(e);
        return false;
    }

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        SetContentView (Resource.Layout.Main);
        _gestureDetector = new GestureDetector(this);

    }
}
Run Code Online (Sandbox Code Playgroud)

因为ViewPager你可以用这样的东西重叠页面PageTransformation

using System;

using Android.Views;
using Android.Support.V4.View;

namespace SomeName
{
public class SinkAndSlideTransformer : Java.Lang.Object, ViewPager.IPageTransformer
{
public void TransformPage(View view, float position)
{
if (position < -1 || position > 1) 
{
view.Alpha = 0; // The view is offscreen.
} 
else
{
view.Alpha = 1;

if (position < 0) 
{
// 'Sink' the view if it's to the left.
// Scale the view.
view.ScaleX = 1 - Math.Abs (position);
view.ScaleY = 1 - Math.Abs (position);

// Set the translationX to keep the view in the middle.
view.TranslationX = view.Width * Math.Abs (position);
} 
}
}
}
}
Run Code Online (Sandbox Code Playgroud)

这会产生这样的效果:
在此输入图像描述

有了这个Adapter

using System;
using System.Collections.Generic;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Support.V4.View;

using Square.Picasso;

namespace StaffPro
{
public class SlideshowPagerAdapter : PagerAdapter
{
    List<Uri> _items = new List<Uri>();
    private readonly Activity _context;

    public SlideshowPagerAdapter (Activity context, List<Uri> items) : base()
    {
        _items = items;
        _context = context;
    }
    public override int Count 
    {
        get { return _items.Count; }
    }
    public override bool IsViewFromObject(View view, Java.Lang.Object _object) 
    {
        return view == ((RelativeLayout) _object);
    }
    public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
    {
        var view = LayoutInflater.From (container.Context).Inflate (Resource.Layout.SlideshowViewPager, container, false);
        ImageView imageView = view.FindViewById<ImageView> (Resource.Id.slideshowImageView);
        Picasso.With(_context).Load(_items [position].ToString()).Into(imageView);
        container.AddView(view);

        return view;
    }
    public override void DestroyItem(ViewGroup container, int position, Java.Lang.Object _object)
    {
        container.RemoveView((View)_object);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

并使用如下方式调用视图页面和适配器:

var viewPagerAdapter = new SlideshowPagerAdapter(Activity, some_Uri_images);
        slideshowViewPager.Adapter = viewPagerAdapter;
        slideshowViewPager.SetPageTransformer (true, new SinkAndSlideTransformer ());
Run Code Online (Sandbox Code Playgroud)

您可以在这里找到更多转换

另请记住,ViewPager有一个名为 的方法setPageMargin()。此方法可以接收负值,这将使片段/视图彼此重叠。
您还可以view.Rotation在变换代码中使用来实现您想要的照片随机放置和重叠效果。
如果您需要保留许多已加载和显示的图像,请检查ViewPager.OffscreenPageLimit它还将覆盖存储下一个和上一个项目的页面的默认缓存限制,但请注意高内存使用量。