Swipe on ListAdapter

Hello Guys...

I am a newbie to xamarin and want to seek advice on following.

I have created an app which ListOut set peoples names and images.Now my requirement is when user swipes on cell of a person it should show details of that paticular person.

Following are codes which I tried.

public class MyScheduleActivity : ListActivity, View.IOnTouchListener { UserDetails noterep = new UserDetails(); List<UserModel> item = new List<UserModel>(); Android.Views.ViewGroup parent; Android.Views.View convertView; private Activity activity; private TextView _myButton; private float _viewX; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); item = noterep.GetAllNotes(); ListAdapter = new NoteAdapter(this, this, Resource.Layout.NoteListRow, item.ToArray()); activity = this; var view = (convertView ?? activity.LayoutInflater.Inflate(Resource.Layout.NoteListRow, parent, false)) as LinearLayout; _myButton = view.FindViewById<TextView>(Resource.Id.myView); _myButton.SetOnTouchListener(this); } public bool OnTouch(View v, MotionEvent e) { switch (e.Action) { case MotionEventActions.Down: _viewX = e.GetX(); break; case MotionEventActions.Move: var left = (int)(e.RawX - _viewX); var right = (left + v.Width); v.Layout(left, v.Top, right, v.Bottom); break; } return true; } }

Any Kind of help and suggestions are welcomed.


  • CortezCortez USMember ✭✭✭

    Hello my friend @Tinku!

    I made a very simple project. The Main.axml, the layout doesn't matter now. This example only detects the swipe (with direction) on it.

    It is suitable for listview items i think. This layout could be very easily a listview item to. A listview item's custom layout is a layout(Linear/Relative/Table) with views on it(TextView, Button, EdiText, etc.). You can listening the gesture in the adapter for example.

    If you want to listening the gesture in the whole listview i can suggest you the ListView.PointToPosition() method.


    With this you can detect the item where you swipe. In the GestureListener.cs you can extend the two event to give back the x and y position. With that you can detect the listview item easily i think.

  • CortezCortez USMember ✭✭✭

    Almost forget, the source :).

  • tinku.efernstinku.eferns USMember

    @Cortez it worked for me

    thank you very much.....

  • tinku.efernstinku.eferns USMember
    edited January 2014

    @Cortez First of all, the eample you gave was quite a big start for me. Thanks once again

    I followed your instruction on using


    Your example works fine with only one listItem. As of you can see I am using List adapter to view the list. I am attaching my arrayListAdapter which return the view. Could you please take a look and give me some advice on it as you did earlier on how to use gestureDetector on it.

  • tinku.efernstinku.eferns USMember

    @Cortez, Let me try out and i will let you know soon.

    Thanks again.

    C U soon

  • tinku.efernstinku.eferns USMember
    edited January 2014


    Your example is really cool.

    I implemented it to my working project but it shows an exception unhandled error. It is beacause in your example you were using "Built-in Row Views" the SimpleListItem1

    As for my requirement my view should have two textView and an imageView. so I followed this link.



        protected override void OnCreate(Bundle savedInstanceState)
             ListView listView;
            _gestureListener = new GestureListener();
            _gestureListener.SwipeLeftEvent += GestureLeft;
            _gestureListener.SwipeRightEvent += GestureRight;
            _gestureDetector = new GestureDetector(this, _gestureListener);
            item = noterep.GetAllNotes();
            ListAdapter = new NoteAdapter(this, this, Resource.Layout.NoteListRow, item.ToArray());

    The commented code throws the error.

    Do you have any idea.

  • tinku.efernstinku.eferns USMember

    Sorry for Delay..

    Upto this it works good

    thanks, you were great help

    See you then

  • norbitharrnorbitharr USMember

    Hello Guys,
    I am very new to xamarin and quite new to android I have been following this discussion for my very first App

    I met almost my requirement of swiping the row to drag another layout

    but that worked only for first three rows. On later rows if i Swipe on one row the layout drags from another row below

    I am attaching my code and screenShots of the same

    Any Ideas are welcomed.

  • CortezCortez USMember ✭✭✭

    Hello @norbitharr!

    For me my code is working. I experienced that for the last row the ListView.PointToPosition returns -1, but you can easly handle that. For any other row i works perfectly.

    I saw your code and try to reproducate it to a working example but it's too complicated. A lot of missing class, resources. I can copy my files there but i don't know is the problem in those files and they are not enough.

    What is the problem exactly what do you want to show on your screenshots? You click in the second listview item's three points and the datas appears at the wrong position, at the previous listview item?

    What is the meaning of the second screenshot, made a circle around the first row three points?

  • norbitharrnorbitharr USMember

    Hello @Cortez

    Sorry For the Confusion

    Actually Exactly what I want is when I Swipe on the three Dots(Circled in Second Image) It should Drag the LinearLayout and output should be Like image 1

    Actually I've its working fine with three rows but then on swiping fourth item's three points the datas appears at the wrong position.

    In following Screen I swiped On Varsha(The First row in Screen Shot) and data Showed at Last Row with name John

    on Alert box you can seen data "SNJohn 3" meaning "SNJohn" at postion "3".

    Hope Now you understand the issue

  • CortezCortez USMember ✭✭✭

    Yeah absolutely.

    I fixed the last row problem and added more element to the listview(sum 15) and every row working perfectly.

  • norbitharrnorbitharr USMember

    What Changes you did you made.

    Could help with some code snippet?

  • CortezCortez USMember ✭✭✭

    I am from home and my code is in my workplace. I didn't change so much. I just added more items to the list as you see in the screenshot

    animals.Add(new Animal("Pooh6", "Disney's daughter"));

    something like this. I think the list filling do a method in the MainActiviy.cs

    Another modification is when i get the position from the listview:

    int position = listView.PointToPosition ((int)second.GetX (), (int)second.GetY ()); if (position == -1) { position = animals.Count; }

    This happen at the last row. The position will be -1 and it handle this.

  • AliSafariMoghaddamAliSafariMoghaddam USMember ✭✭

    Hi Cortzur i have seen and run your code, but there is error here:'GestureRight' matches delegate 'System.Action' and also for Left part is the same, how to can solve it?

  • CortezCortez USMember ✭✭✭


    Did you write the code correctly? Because for me it's working for me. I didn't get this kind of exception.

  • Hello Balazs Kaposztas,

    This is nice work you shared with us. It helped a lot for me too. but can you please tell me that how can i add some animation while swiping an item, so that it will look like actual swiping...

  • HridayeshChaudharyHridayeshChaudhary USMember
    edited May 2014
    void GestureLeft(MotionEvent first, MotionEvent second)
        int position = listview.PointToPosition ((int)second.GetX (), (int)second.GetY ());
        Toast.MakeText (this, lstcategory [position - 1].CategoryName + " is deleted.", ToastLength.Long).Show ();

    this method is returning -1 position for last element in the list-view.
    Anyone with any solution...?

    Thank you...

  • adeelnasiradeelnasir QAMember

    Hi Cortez,

    Thanks for sharing this swipe code, it works great, I am using it on an activity to show different content in a text view on swipe. It is working fine by changing the contents on left/right swipe. But I dont get a swipe feel, do I need to start different activities on left/right swipe? I want to use the same activity but with different data and it should feel like the screen has swiped. Can you please guide me on this?


  • KeyzerSKeyzerS NOMember ✭✭

    You are awesome, Cortez! Every time im stuck with something, and googling like crazy, i end up on a thread where you solve it in a good and understandabler way. Thanks a lot!

  • KrishnakKrishnak USMember

    Even though i swipe on items within the list not the last one, Some times the _listView.PointToPosition((int)second.GetX(), (int)second.GetY()); is returning -1. Any ideas on this?

  • I have the same question of "Krishnak".
    My listview is always returning "-1" as well.


    Hi All,

    How can I Implement Left Swipe to Delete in Xamarin Android ???

  • Please Help ME
    How can I Implement Left Swipe to Delete in Xamarin Android ???

  • BenMessengerBenMessenger AUUniversity ✭✭

    Hi Cortez,
    Like others I just want to say thanks for the class and code. You are a champion. Too easy.
    And I can also view the source and learn. I find a lot of the Xamarin recipes a bit vague, so your help is outstanding.

  • SaharPournassehSaharPournasseh USMember ✭✭

    @Cortez Thanks for the code, it's just awesome!
    but I need some help implementing it in a fragment. How can I implement DispatchTouchEvent inside my fragment?

  • AswathiPSAswathiPS USMember ✭✭

    hi, any one share how it is possible in xamarin cross platform

Sign In or Register to comment.