使用C# - Xamarin覆盖片段中的bool

时间:2018-05-18 07:21:54

标签: c# android visual-studio xamarin fragment

我的代码有问题。覆盖bool在片段中不起作用,但是当我在活动上尝试它时。它完美地运作。错误说"找不到合适的方法来覆盖"。我不知道为什么覆盖bool在片段中不起作用。

Here is the picture of the error

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using SupportFragment = Android.Support.V4.App.Fragment;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Support.V4.Widget;
using Android.Support.V4.App;
using Android.Views.Animations;

namespace EFCAndroid
{
    public class Fragment_menu : SupportFragment
    {
        private float initialX;
        private ViewFlipper viewflipper;
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your fragment here
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // Use this to return your custom view for this Fragment
            // return inflater.Inflate(Resource.Layout.YourFragment, container, false);
            View view = inflater.Inflate(Resource.Layout.fragment_menu, container, false);

            viewflipper = view.FindViewById<ViewFlipper>(Resource.Id.viewFlipper);

            viewflipper.SetInAnimation(Activity, Resource.Animation.fade_in);
            viewflipper.SetOutAnimation(Activity, Resource.Animation.fade_out);
            viewflipper.SetFlipInterval(5000);
            viewflipper.StartFlipping();

            return view;

        }

        public override bool OnTouchEvent(MotionEvent ontouch)
        {

            //switch (ontouch.Action)
            switch(ontouch.Action)
            {
                case MotionEventActions.Down:
                    initialX = ontouch.GetX();
                    break;
                case MotionEventActions.Up:
                    float finalX = ontouch.GetX();
                    if (initialX > finalX)
                    {
                        if (viewflipper.DisplayedChild == 1)
                            break;
                        //viewflipper.SetInAnimation(this, Resource.Animation.fade_in);
                        //viewflipper.SetInAnimation(this, Resource.Animation.fade_out);
                        viewflipper.ShowNext();
                    }
                    else
                    {
                        if (viewflipper.DisplayedChild == 0)
                            break;
                        //viewflipper.SetInAnimation(this, Resource.Animation.fade_in);
                        //viewflipper.SetInAnimation(this, Resource.Animation.fade_out);
                        viewflipper.ShowPrevious();
                    }
                    break;
            }
            return false;
        }

    }
}

2 个答案:

答案 0 :(得分:0)

碎片并没有被替换为ontouchevent。也许你可以使用这个链接How to handle Touch Events on a Fragment?

答案 1 :(得分:0)

这个link正在使用java。转换成c#后,它应该是这样的:

using Android.App;
using Android.OS;
using Android.Util;
using Android.Views;

namespace FragmentTest
{
    public class Fragment1 : Fragment,View.IOnTouchListener
    {
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your fragment here
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // Use this to return your custom view for this Fragment
            var view = inflater.Inflate(Resource.Layout.layout1, container, false);
            view.SetOnTouchListener(this);
            return view;


        }

        public bool OnTouch(View v, MotionEvent e)
        {
            if (e.Action == MotionEventActions.Move){
                //do something 
                Log.Error("lv","MOVE++++++++++++++++++");
                }

            return true;
        }
    }
}

效果很好。

因为Fragment中没有OnTouchEvent方法,所以我们需要自己实现它。或者通过定义界面来实现它,并使用dispatchTouchEvent方法,它比上面的代码更复杂。