Exemplo n.º 1
0
        public override bool Fling(int velocityX, int velocityY)
        {
            if (ItemCount < 1)
            {
                return(true);
            }
            FlingEventArgs flingEventArgs;

            if (velocityX > 0)
            {
                flingEventArgs = new FlingEventArgs(FlingDirection.Right, CurrentItem);
                if (CurrentItem == ItemCount - 1)
                {
                    return(true);
                }
            }
            else
            {
                flingEventArgs = new FlingEventArgs(FlingDirection.Left, CurrentItem);
            }

            if (BeforeFling != null)
            {
                BeforeFling(this, flingEventArgs);
            }
            LinearLayoutManager linearLayoutManager = (LinearLayoutManager)GetLayoutManager();

            //these four variables identify the views you see on screen.
            int  lastVisibleView  = linearLayoutManager.FindLastVisibleItemPosition();
            int  firstVisibleView = linearLayoutManager.FindFirstVisibleItemPosition();
            View firstView        = linearLayoutManager.FindViewByPosition(firstVisibleView);
            View lastView         = linearLayoutManager.FindViewByPosition(lastVisibleView);

            //these variables get the distance you need to scroll in order to center your views.
            //my views have variable sizes, so I need to calculate side margins separately.
            //note the subtle difference in how right and left margins are calculated, as well as
            //the resulting scroll distances.
            int leftMargin          = (screenWidth - lastView.Width) / 2;
            int rightMargin         = (screenWidth - firstView.Width) / 2 + firstView.Width;
            int leftEdge            = lastView.Left;
            int rightEdge           = firstView.Right;
            int scrollDistanceLeft  = leftEdge - leftMargin;
            int scrollDistanceRight = rightMargin - rightEdge;

            //if(user swipes to the left)
            if (velocityX > 0)
            {
                CurrentItem = lastVisibleView - 1;
                SmoothScrollBy(scrollDistanceLeft, 0);
            }
            else
            {
                CurrentItem = firstVisibleView - 1;
                SmoothScrollBy(-scrollDistanceRight, 0);
            }

            if (OnFling != null)
            {
                OnFling(this, flingEventArgs);
            }
            return(true);
        }
 private void OnRecyclerViewFling(object sender, FlingEventArgs e)
 {
 }