![]() Inside the ACTION_MOVE block, we first initialise the newX variable that holds the new x-coordinate that the card has been swiped to. Now, let's implement the code for the ACTION_MOVE event. Essentially, this gives us the x-coordinate of this position of the card: Starting position of card. ![]() ![]() Then we get the cardWidth using the width property of the quoteCard.įinally, we calculate the starting position of the card using the formula (width of screen/2) - (cardWidth/2). Val cardStart = (() / 2) - (cardWidth / 2)įirst we get the displayMetrics from resources, which will give us the width of the screen using (). Val displayMetrics = resources.displayMetrics variables to store current configuration of quote card. Place these lines of code before the when(event.action) statement: tOnTouchListener( We will just make sure that it is calculated with respect to the full-screen width, not just the card's width. To implement the logic, we first need to have the starting position of the card, which is fairly easy to calculate. You guessed it – we will calculate the mean position and on the ACTION_MOVE event, we will check if the user swiped to the left and move the card accordingly. Swipe only if moved towards the left of mean position We want the card to swipe if and only if the user swipes it to the left of the mean position. Let's define the mean position as the center of the card. Now, to achieve this, let's think of it in terms of the card. We want users to be able to swipe it only to the left, and if the minimum threshold to load a new quote is reached, it should move back to its original position and load a new quote. Your code will also make more sense when your requirements are clear. Implementing functionality is easier when you know exactly what you wish to have. ![]() The math behind the swipe actionįirst, let's re-think what we want to achieve. The basic setup for the card is done, so let's figure out the swiping logic. There are many other action events that we can override, such as ACTION_DOWN that's called when a person gets hold of the view, but we don't need them for this feature.
0 Comments
Leave a Reply. |