1
votes

I have the following list of item in my viewmodel. I am showing one item at a time in the View. Lets say first item which is A. But When user swipes to right or left, I want View to refresh and show the next item, for example B.

I wonder how swipe right or left achievable to iterate through the list item using mvvmcross? Even to know direction of swipe would be more than enough.

I am looking at the following url to learn MvxGestureRecognizerBehavior but it is broken

https://github.com/MvvmCross/MvvmCross-Build/blob/master/MvvmCross/Cirrious/Cirrious.MvvmCross.Binding.Touch/Views/Gestures/MvxGestureRecognizerBehavior.cs

public RViewModel()
 {
    Items = new ObservableCollection<ListItem> {
        new ListItem { Title = "A" },
        new ListItem { Title = "B" },
        new ListItem { Title = "C" },
        new ListItem { Title = "D" },
        new ListItem { Title = "E" }
    };
 }

Note: Please keep in mind, I could able to use viewpager but since I am loading the similar content (just TextView) in the View, I do not want to create multiple fragments to iterate through each item. Therefore, I only want to refresh that TextView / View with left/right swipe action.

1
I think you're looking for this [file]( github.com/MvvmCross/MvvmCross-Build/blob/…), but seems to be for IOS. As for the question I'm afraid I can't help, I'm not an expert on MvvmCross - Ric .Net
That is iOS, I am looking for Android. Thanks Ric.Net. - casillas
Hi @casillas. I know it's an old question but I had a question that did you put animation when swiping from left to right or right to left? - Nick King

1 Answers

2
votes

Create an interface IMotionViewModel with an method for handling the swipe. Apply it to the ViewModel

In your droid activity implement the GestureDetector.IOnGestureListener interface. Handle the motion event where you require and access the ViewModel there and pass the motion or arguments there.

ViewModel

public interface IMotionViewModel
{
    void OnSwipe(bool swipeRight);
}

public class FirstViewModel : MvxViewModel, IMotionViewModel
{
    public ObservableCollection<ListItem> Items { get; set; }
    private string _currentString = "Hello MvvmCross";
    public string CurrentString
    {
        get { return _currentString; }
        set { SetProperty(ref _currentString, value); }
    }

    public FirstViewModel()
    {
        Items = new ObservableCollection<ListItem> {
                new ListItem { Title = "A" },
                new ListItem { Title = "B" },
                new ListItem { Title = "C" },
                new ListItem { Title = "D" },
                new ListItem { Title = "E" }
            };
    }

    public void OnSwipe(bool swipeRight)
    {
        //do list iteration here....
    }
}

Droid Activity

[Activity(Label = "View for FirstViewModel")]
public class FirstView : MvxActivity, Android.Views.GestureDetector.IOnGestureListener
{
    private GestureDetector _gestureDetector;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.FirstView);
        _gestureDetector = new GestureDetector(this);
    }

    public override bool OnTouchEvent(MotionEvent e)
    {
        _gestureDetector.OnTouchEvent(e);
        return false;
    }

    public bool OnDown(MotionEvent e)
    {
        return false;
    }

    public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        var isSwipingRight = (e2.RawX - e1.RawX) > 0 ? true : false;

        var motionVm = base.ViewModel as IMotionViewModel;
        motionVm.OnSwipe(isSwipingRight);
        return true;
    }

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