Skip to content

Instantly share code, notes, and snippets.

@NathanWalker
Created April 21, 2020 01:36
Show Gist options
  • Save NathanWalker/56944e0966087dccdb99e876abd8b8a2 to your computer and use it in GitHub Desktop.
Save NathanWalker/56944e0966087dccdb99e876abd8b8a2 to your computer and use it in GitHub Desktop.
Invert NativeScript ListView trick to feed chat items in from bottom up (Android workaround for proper momentum scroll on API level 28)
export function fixAndroidScrollMomentum(listview: ListView) {
if (android.os.Build.VERSION.SDK_INT !== 28) {
return;
}
// suppress the default momentum behaviour
listview.android.setVelocityScale(0);
// stores the last 5 move events in this array
let lastMoveEvents = [];
(<any>listview).on(GestureTypes.touch, (args, b) => {
if (args.action === 'up') {
const eventCount = lastMoveEvents.length;
if (eventCount < 2) {
return;
}
// calculate velocity based on the fist and last recorded move events.
const flingVelocity =
(lastMoveEvents[eventCount - 1].y - lastMoveEvents[0].y) * (lastMoveEvents[eventCount - 1].time - lastMoveEvents[0].time) * -1;
// apply the velocity
listview.android.fling(flingVelocity);
} else if (args.action === 'move') {
// add the move event to the array and crop its length to 5.
lastMoveEvents.unshift({
x: args.getX(),
y: args.getY(),
time: Date.now()
});
lastMoveEvents = lastMoveEvents.slice(0, 5);
} else if (args.action === 'down' || args.action === 'cancel') {
lastMoveEvents = [];
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment