0

I have some problem with my codes. The override bool is not working in the fragment but when I try it on the activity. It works perfectly. The error says "No suitable method found to override". I don't know why the override bool is not working in the fragment.

Here is the picture of the error

Code:

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

    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    Fragment doesn't have ontouchevent method – Vincent Elbert Budiman May 18 '18 at 07:32
  • Hi. I try the example that your gave to me. But still not working. I put override inside the view.setontouchlistener. But still got the error says No suitable method found to override :3 – Jayson E Garcia May 18 '18 at 07:40
  • Does my answer work for you? – Robbit May 22 '18 at 09:41
  • Still error when i write the (new View.IOnTouchListener() There is no function called view.ontouchlistener() in c# :( – Jayson E Garcia May 23 '18 at 01:55
  • 1
    It is not `view.ontouchlistener()`, it is `view.SetOnTouchListener();`, note the package, it is `using Android.Views;`. [Here](https://developer.xamarin.com/api/member/Android.Views.View.SetOnClickListener/p/Android.Views.View+IOnClickListener/) you can see the method in c#. – Robbit May 31 '18 at 08:55

2 Answers2

0

Fragment doesn't have ontouchevent to be replaced. maybe you can use this link How to handle Touch Events on a Fragment?

  • Hi. I try the example that your gave to me. But still not working. I put override inside the view.setontouchlistener. But still got the error says No suitable method found to override :3 – Jayson E Garcia May 18 '18 at 07:42
0

This link is using java. After translate into c#, it should like this:

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

It works well.

Because, there is no OnTouchEvent method in Fragment, so we need implement it by ourselves. Or there is another way to achieve it by define an interface, and use dispatchTouchEvent method, it's more complex than above codes.

Community
  • 1
  • 1
Robbit
  • 4,300
  • 1
  • 13
  • 29