UI Scrollable List

From GiderosMobile

Here you will find various resources to help you create scrollable lists in Gideros Studio.

note: you may have to provide your own assets (fonts, gfx, …)

note2: this class uses the GTween class which uses the Easing Gideros Studio plugin!

Scrollable List

Class

--====================================================================--        
-- ListView widget for Gideros
--====================================================================--
--
-- ListView.lua
-- Version 0.1 beta
-- Author: Lukas Gergel, http://pykaso.net
-- 
-- This library is free to use and modify. Use it for free!
--
-- TODO: add methods for programatic manipulating with ListView
--
-- USAGE
--
-- myList = ListView.new({
--	width=280,
--	height=390,
--	bgTexture = Texture.new("texture.png"),
--  bgColor = 0xffffff,
--  rowSnap = true, -- experimental feature
--  friction = 0.92 -- number lower then 1
--	data=data
-- })

ListView = Core.class(Shape)

function ListView:init(params)
	-- properties
	self.velocityPrevTime = 0
	self.listHeight = 0

	--data items (pre rendered sprites)
	local data = params.data or {}

	--configuration
	self.cfg = {
		--dimension
		width = params.width or self.screenW,
		height = params.height or self.screenH,
		bgColor = params.bgColor or nil,
		rowSnap = params.rowSnap or false,
		friction = params.friction or 0.92,
		callback = params.callback or function(row) return row end,
		onScroll = params.onScroll or nil
	}

	local prevY, prevH, i, finalData = 0, 0, 0, {}

	for i = 1, #data do
		finalData[i] = {}
		finalData[i].objectData = data[i]
		finalData[i].height = data[i]:getHeight()
		finalData[i].xInit = 0
		finalData[i].yInit = prevY + prevH
		prevY = finalData[i].yInit
		prevH = finalData[i].height
		self.listHeight = self.listHeight + finalData[i].height
	end

	if(self.cfg.bgColor ~= nil) then
		self:setFillStyle(Shape.SOLID, self.cfg.bgColor, 1)
	end

	-- create defined shape
	self:beginPath()
	self:moveTo(0, 0)
	self:lineTo(0, self.cfg.height)
	self:lineTo(self.cfg.width, self.cfg.height)
	self:lineTo(self.cfg.width, 0)
	self:closePath()
	self:endPath()
	
	-- create defined shape
	self.mask = Shape.new()
	self.mask:beginPath()
	self.mask:moveTo(0, 0)
	self.mask:lineTo(0, self.cfg.height)
	self.mask:lineTo(self.cfg.width, self.cfg.height)
	self.mask:lineTo(self.cfg.width, 0)
	self.mask:closePath()
	self.mask:endPath()
	self:addChild(self.mask)

	self.listItems = Sprite.new()
	self:addChild(self.listItems)
	self.viewSize = self.cfg.height

	self.itemData = finalData
	finalData = nil
	self:createRender()
end

function ListView:newListItem(id, data)
	local thisItem = Sprite.new()
	local callback = self.cfg.callback
	local t = callback(data)
	local tv = self
	thisItem.id = id
	thisItem:addChild(t)

	thisItem:addEventListener(Event.TOUCHES_BEGIN, function(e)
		if(self.mask:hitTestPoint(e.touch.x, e.touch.y)) then
			tv:listItemTouch(e, "begin")
		end
	end)

	thisItem:addEventListener(Event.TOUCHES_MOVE, function(e)
		tv:listItemTouch(e,"move")
	end)

	thisItem:addEventListener(Event.TOUCHES_END, function(e)
		tv:listItemTouch(e,"end")
	end)

	return thisItem
end

function ListView:listItemTouch(e, act)
	local li = self.listItems
	local top, height = self:getY(), self:getHeight()

	if(act == "begin") then
		delta, velocity, prevPos = 0, 0, 0
		self.isFocus = true

		startPos = e.touch.y
		prevPos = e.touch.y

		if self.tween then
			self.tween:setPaused(true)
			self.tween = nil
		end

		self:removeEventListener("enterFrame", self.scrollList, self)
		self:addEventListener("enterFrame", self.trackVelocity, self)

	elseif(self.isFocus) then
		if(act == "move") then
			local lastItem = li:getChildAt(li:getNumChildren())
			delta = e.touch.y - prevPos
			prevPos = e.touch.y

			if (li:getChildAt(1).id == 1 and li:getY() > 0) or
					(lastItem.id == #self.itemData and
					(self.listHeight - lastItem:getY() - lastItem:getHeight()) <= 0) then
				li:setY(math.floor(li:getY() + delta / 3))
			else
				li:setY(math.floor(li:getY() + delta))
			end
			self:updateRender()
		end

		if(act == "end") then
			self:removeEventListener("enterFrame", self.trackVelocity, self)
			self:addEventListener("enterFrame", self.scrollList, self)
			self.isFocus = false
		end
	end
end

function ListView:scrollList(event)
	if math.abs(velocity) < 0.3 then
		velocity = 0
		self:removeEventListener("enterFrame", self.scrollList, self)

		-- experimental feature
		if self.cfg.rowSnap then
			local _me = self
			local lastItem = self.listItems:getChildAt(self.listItems:getNumChildren())
			local firstVisibleItem = self:getFirstVisibleRow()

			if firstVisibleItem ~= nil and self.listItems:getY() < 0 and
					self.listHeight - lastItem:getY() - lastItem:getHeight() > 0 then
				local x, y = firstVisibleItem:getBounds(self)
				local vx, vy = self.listItems:localToGlobal(self.listItems:getBounds(self))
				local final = self.listItems:getY()
				if firstVisibleItem:getHeight() + y >= firstVisibleItem:getHeight() / 2 then
					final = self.listItems:getY() - y
				else
					final = self.listItems:getY() - (firstVisibleItem:getHeight() + y)
				end
				self.tween = GTween.new(self.listItems, 0.7, {y = final}, {delay = 0.01,
						ease = easing.outQuartic, onChange = function() self:updateRender() end})
			end
		else
			-- nothing here
		end
	end

	local timePassed = event.deltaTime * 300
	local li = self.listItems

	-- Slow the list down
	velocity = velocity * self.cfg.friction
	li:setY(math.floor(self.listItems:getY() + velocity * timePassed))

	self:updateRender()

	local firstItem = li:getChildAt(1)
	local lastItem = li:getChildAt(li:getNumChildren())
	local lx, ly = self:localToGlobal(li:getBounds(self))
	local x, y, w, h = self:getBounds(self)
	local x2, y2 = self:localToGlobal(x, y)
	--local lx, ly = lastItem:getBounds(self)

	if firstItem.id == 1 and li:getY() > 0 then
		velocity = 0
		self:removeEventListener("enterFrame", self.scrollList, self)
		self.tween = GTween.new(li, 0.4, {y = li.yInit}, {delay = 0.01, ease = easing.outQuartic})
	end

	if lastItem.id == #self.itemData and self.listHeight - lastItem:getY() - lastItem:getHeight() <= 0 then
		if self.listHeight > self.viewSize and li:getY() < -self.listHeight + self.viewSize - lastItem:getHeight() * 0.5 then
			velocity = 0
			self:removeEventListener("enterFrame", self.scrollList, self)
			self.tween = GTween.new(li, 0.4, {y = math.ceil(self.viewSize - self.listHeight)},
					{delay = 0.01, ease = easing.outQuartic})
		elseif self.listHeight < self.viewSize then
			velocity = 0
			self:removeEventListener("enterFrame", self.scrollList, self)
			self.tween = GTween.new(li, 0.4, {y = li.yInit}, {delay = 0.01, ease = easing.outQuartic})
		end
	end

	return true
end

function ListView:getFirstVisibleRow()
	local index = 1
	local posY = self:getY()
	local item = nil

	while posY <= self:getY() do
		if(index < self.listItems:getNumChildren()) then
			item = self.listItems:getChildAt(index)
			local x1, y1 = self:localToGlobal(item:getBounds(self))
			posY = y1 + item:getHeight()
			index = index + 1
		else
			return nil
		end
	end

	return item
end

function ListView:createRender()
	local lastY = 0
	local position = 1
	while lastY < self.viewSize and position <= #self.itemData do
		local rowObject = self:render(nil, position)
		if (rowObject == nil) then break end
		self.listItems:addChild(rowObject)
		rowObject:setPosition(self.itemData[position].xInit, self.itemData[position].yInit)
		lastY = rowObject:getY()
		position = position + 1
	end
	self.listItems.yInit = self.listItems:getY()
end

function ListView:render(thisItem, id)
	if thisItem == nil then
	    thisItem = self:newListItem(id, self.itemData[id].objectData)
	else
		thisItem:removeChildAt(thisItem:getNumChildren())
		local callback = self.cfg.callback
		local t = callback(self.itemData[id].objectData)
		thisItem:addChild(t)
		thisItem.id = id
	end

	return thisItem
end

function ListView:updateRender()
	local firstItem = self.listItems:getChildAt(1)
	local lastItem = self.listItems:getChildAt(self.listItems:getNumChildren())
	local fx, fy = firstItem:getBounds(self)
	local lx, ly = lastItem:getBounds(self)
	local bottom = self.viewSize + self:getY()
	local fillToTop = fy > 0
	local fillToBottom = ly + lastItem:getHeight() < self.viewSize

	if fillToBottom then
		if (ly + lastItem:getHeight() < self.viewSize) and (lastItem.id ~= #self.itemData) then
			local y = lastItem:getY() + lastItem:getHeight()
			local recycledRow = firstItem
			self:render(recycledRow, lastItem.id + 1)
			self.listItems:addChild(recycledRow)
			recycledRow:setY(y)
		end

	elseif fillToTop then
		if fy > 0 and (firstItem.id ~= 1) then
			local recycledRow = lastItem
			local newItem = self:render(recycledRow, firstItem.id - 1)
			local y = firstItem:getY() - newItem:getHeight()
			self.listItems:addChildAt(recycledRow, 1)
			recycledRow:setY(y)
		end
	end

	if (self.cfg.onScroll ~= nil) then
		self.cfg.onScroll(self, firstItem)
	end
end

function ListView:trackVelocity(event)
	local timePassed = msTimer() - self.velocityPrevTime
	self.velocityPrevTime = self.velocityPrevTime + timePassed

	if prevY then
		velocity = (self.listItems:getY() - prevY) / timePassed
	end
	prevY = self.listItems:getY()
end

function msTimer()
	return math.floor(os.timer() * 500)
end

return ListView

Example

require "easing" -- don't forget to add the GTween class too!

-- fonts
local font = TTFont.new("fonts/Roboto-Medium-webfont.ttf", 24)
local fontSub = TTFont.new("fonts/Roboto-Medium-webfont.ttf", 18)

-- rows layout
function row(item)
	local row = Sprite.new()

	local itembmp = Bitmap.new(Texture.new(item.icon))
	itembmp:setScale(0.75)
	itembmp:setPosition(0, 0)
	row:addChild(itembmp)

	local itemtitle = TextField.new(font, item.title)
	itemtitle:setPosition(itembmp:getX() + itembmp:getWidth() + 4, itembmp:getY() + itemtitle:getHeight())
	row:addChild(itemtitle)

	-- clickable button (arrow button)
	local arrow = Texture.new("gfx/arrow_right.png")
	local arrow_pressed = Texture.new("gfx/arrow_right_down.png")
	local button = Button.new(Bitmap.new(arrow), Bitmap.new(arrow_pressed))
	button:setScale(3, 2)
	button:setPosition(application:getContentWidth() - button:getWidth(), itembmp:getY())
	button:addEventListener("click", function()
		mytitle:setText(item.title)
	end)
	row:addChild(button)

	return row
end

-- datas
local data = {}
for i = 1, 10 do
	data[i] = row(
		{
			icon = "gfx/maurice.png",
			title = "Item ".. i,
		}
	)
end

-- scrollable list
local myList = ListView.new(
	{
		width = application:getContentWidth() - 32,
		height = 8 * application:getContentHeight() / 10,
		friction = 0.99,
		bgColor = 0xaaaaff,
		rowSnap = true, -- experimental feature
		data = data
	}
)
myList:setPosition(16, 0)

-- infos
mytitle = TextField.new(nil, "CLICK AN ARROW")
mytitle:setScale(3)
mytitle:setTextColor(0xff0000)
mytitle:setPosition(0, application:getContentHeight() - mytitle:getHeight())

-- stage
stage:addChild(myList)
stage:addChild(mytitle)

Scrollable List AI

Class (you can pick your variation)

ScrollableGridAI = Core.class(Sprite) -- AI is MS Copilot via MS Edge!

local pow = math.pow

function ScrollableGridAI:init(xparams)
	xparams = xparams or {}
	self.params = {}
	-- config
	self.params.data = xparams.data or {} -- Table of sprites
	self.params.contentheight = xparams.contentheight or 64
	local cellwidth = xparams.cellwidth or 0
	self.params.numcolumns = xparams.numcolumns or 4 -- 4
	self.params.itemspacing = xparams.itemspacing or 2 -- 0
	self.params.sbwidth = xparams.sbwidth or 32 -- 16, scrollbar width
	-- keyboard navigation for scrolling
	self.isup, self.isdown = false, false
	self.isqup, self.isqdown = false, false -- quick up and down (PAGE_UP, PAGE_DOWN)
	-- create bg
	local cw = (cellwidth+self.params.itemspacing)*self.params.numcolumns -- contentwidth
	local bg = Pixel.new(
		0xffffff, 0.25, -- magik, add to params?
		cw+self.params.sbwidth,
		self.params.contentheight
	)
	bg:setX(-self.params.sbwidth/2)
	self:addChild(bg)
	-- Create container for content
	self.content = Sprite.new()
	-- supercontent for clipping
	self.supercontent = Sprite.new()
	self.supercontent:setClip(0, 0, cw, self.params.contentheight)
	self.supercontent:addChild(self.content)
	self:addChild(self.supercontent)
	-- Vertical scrollbar setup
	self.scrollbarHeight = 32 -- 0
	self.scrollbar = Shape.new()
	self.scrollbar:setFillStyle(Shape.SOLID, 0x0, 0.7) -- add to params?
	self.scrollbar:beginPath()
	self.scrollbar:moveTo(0, 0)
	self.scrollbar:lineTo(self.params.sbwidth, 0)
	self.scrollbar:lineTo(self.params.sbwidth, self.scrollbarHeight)
	self.scrollbar:lineTo(0, self.scrollbarHeight)
	self.scrollbar:closePath()
	self.scrollbar:endPath()
	self.scrollbar:setX(cw)
	self:addChild(self.scrollbar)
	--
	self.scrollVelocity = 0 -- Momentum state
	self.friction = 0.8 -- 0.9, decay factor per frame
	self.minVelocity = 0.1 -- threshold to stop
	self.maxVelocity = 50  -- cap fling speed
	-- let's go!
	self:loadData(self.params.data)
	-- listeners
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
	self:addEventListener(Event.MOUSE_WHEEL, self.onMouseWheel, self)
	self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
	self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
	self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
	self:addEventListener(Event.KEY_DOWN, self.onKeyDown, self)
	self:addEventListener(Event.KEY_UP, self.onKeyUp, self)
end

-- Load data and arrange in grid
function ScrollableGridAI:loadData(xdata) -- xdata = Table of sprites
	for i = self.content:getNumChildren(), 1, -1 do
		self.content:removeChildAt(i)
	end
	--
	local xPos, yPos = 0, 0
	local colCount = 0
	local maxRowHeight = 0
	for k, v in ipairs(xdata) do
		local spr = xdata[k]
		spr:setPosition(xPos, yPos)
		self.content:addChild(spr)

		if spr:getHeight() > maxRowHeight then
			maxRowHeight = spr:getHeight()
		end

		colCount = colCount + 1
		if colCount < self.params.numcolumns then
			xPos = xPos + spr:getWidth() + self.params.itemspacing
		else
			colCount = 0
			xPos = 0
			yPos = yPos + maxRowHeight + self.params.itemspacing
			maxRowHeight = 0
		end
	end

	-- Scroll limits
	self.maxY = 0
	self.minY = 0 >< (self.params.contentheight - (yPos + maxRowHeight))

	-- Scroll state
	self.startTouchY, self.startContentY = 0, 0
	self.isDraggingContent = false
	self.isDraggingScrollbar = false
	self.startScrollbarY = 0

	-- Vertical scrollbar update
	local totalContentHeight = yPos + maxRowHeight
	self.scrollbarHeight = self.params.contentheight * (self.params.contentheight / totalContentHeight)
	self.scrollbar:clear()
	self.scrollbar:setFillStyle(Shape.SOLID, 0x0, 0.7) -- add to params?
	self.scrollbar:beginPath()
	self.scrollbar:moveTo(0, 0)
	self.scrollbar:lineTo(self.params.sbwidth, 0)
	self.scrollbar:lineTo(self.params.sbwidth, self.scrollbarHeight)
	self.scrollbar:lineTo(0, self.scrollbarHeight)
	self.scrollbar:closePath()
	self.scrollbar:endPath()
	-- update positions
	self.content:setY(0)
	self:updateScrollbarFromContent()
end

-- Function to update scrollbar position from content
function ScrollableGridAI:updateScrollbarFromContent()
	if self.minY < 0 then
		local scrollRatio = (self.content:getY() - self.minY) / (self.maxY - self.minY)
		self.scrollbar:setY((self.params.contentheight - self.scrollbarHeight) * (1 - scrollRatio))
		self.scrollbar:setVisible(true)
	else
		self.scrollbar:setVisible(false)
	end
end

-- Function to update content position from scrollbar
function ScrollableGridAI:updateContentFromScrollbar()
	local ratio = self.scrollbar:getY() / (self.params.contentheight - self.scrollbarHeight)
	local newContentY = self.minY + (self.maxY - self.minY) * (1 - ratio)
	self.content:setY(newContentY)
end

--[[
-- Touch start V1 OK
function ScrollableGridAI:onMouseDown(e)
	if self.scrollbar:hitTestPoint(e.x, e.y) then
		self.isDraggingScrollbar = true
		self.startTouchY = e.y
		self.startScrollbarY = self.scrollbar:getY()
	elseif self.content:hitTestPoint(e.x, e.y) then
		self.isDraggingContent = true
		self.startTouchY = e.y
		self.startContentY = self.content:getY()
	end
end
]]

--[[
-- Touch start V2 OK
function ScrollableGridAI:onMouseDown(e)
    if self.scrollbar:hitTestPoint(e.x, e.y) then
        -- Clicked directly on the scrollbar thumb
        self.isDraggingScrollbar = true
        self.startTouchY = e.y
        self.startScrollbarY = self.scrollbar:getY()
    elseif e.x >= self:getX() + self.scrollbar:getX() and e.x <= self:getX() + self.scrollbar:getX() + self.params.sbwidth then
        -- Clicked on the scrollbar track (outside the thumb)
        local clickY = e.y
        -- Clamp clickY to track bounds
        if clickY < 0 then clickY = 0 end
        if clickY > self.params.contentheight then clickY = self.params.contentheight end

        -- Compute ratio of click position within track
        local ratio = clickY / self.params.contentheight

        -- Move scrollbar thumb so its center aligns with click
        local newScrollY = ratio * (self.params.contentheight - self.scrollbarHeight)
        self.scrollbar:setY(newScrollY)

        -- Update content position accordingly
        self:updateContentFromScrollbar()
    elseif self.content:hitTestPoint(e.x, e.y) then
        -- Dragging content
        self.isDraggingContent = true
        self.startTouchY = e.y
        self.startContentY = self.content:getY()
    end
end
]]

-- Touch start V3 OK
function ScrollableGridAI:onMouseDown(e)
    if self.scrollbar:hitTestPoint(e.x, e.y) then
        -- Clicked directly on the scrollbar thumb
        self.isDraggingScrollbar = true
        self.startTouchY = e.y
        self.startScrollbarY = self.scrollbar:getY()
    elseif e.x >= self:getX() + self.scrollbar:getX() and
		e.x <= self:getX() + self.scrollbar:getX() + self.params.sbwidth then
        -- Clicked on the scrollbar track (outside the thumb)
        local clickY = e.y
        local thumbTop = self.scrollbar:getY()
        local thumbBottom = thumbTop + self.scrollbarHeight

        if clickY < thumbTop then
            -- Page up: move content up by one viewport
            local newY = self.content:getY() + self.params.contentheight
            if newY > self.maxY then newY = self.maxY end
            self.content:setY(newY)
        elseif clickY > thumbBottom then
            -- Page down: move content down by one viewport
            local newY = self.content:getY() - self.params.contentheight
            if newY < self.minY then newY = self.minY end
            self.content:setY(newY)
        end

        -- Sync scrollbar thumb with content
        self:updateScrollbarFromContent()
    elseif self.content:hitTestPoint(e.x, e.y) then
        -- Dragging content
        self.isDraggingContent = true
        self.startTouchY = e.y
        self.startContentY = self.content:getY()
    end
end

-- Touch move
function ScrollableGridAI:onMouseMove(e)
	if self.isDraggingContent then
		local dy = e.y - self.startTouchY
		local newY = self.startContentY + dy
		if newY > self.maxY then newY = self.maxY end
		if newY < self.minY then newY = self.minY end
		self.content:setY(newY)
		self:updateScrollbarFromContent()

		-- Momentum: set velocity based on drag delta
		self.scrollVelocity = dy
		if self.scrollVelocity > self.maxVelocity then self.scrollVelocity = self.maxVelocity end
		if self.scrollVelocity < -self.maxVelocity then self.scrollVelocity = -self.maxVelocity end
	elseif self.isDraggingScrollbar then
		local dy = e.y - self.startTouchY
		local newScrollY = self.startScrollbarY + dy
		if newScrollY < 0 then newScrollY = 0 end
		if newScrollY > self.params.contentheight - self.scrollbarHeight then
			newScrollY = self.params.contentheight - self.scrollbarHeight
		end
		self.scrollbar:setY(newScrollY)
		self:updateContentFromScrollbar()
	end
end

-- Touch end
function ScrollableGridAI:onMouseUp(e)
	self.isDraggingContent = false
	self.isDraggingScrollbar = false
end

-- Mouse wheel scrolling with momentum
function ScrollableGridAI:onMouseWheel(e)
	local baseSpeed = 1/(120*2) -- 1/12000, 1
	local multiplier = pow(-e.wheel<>e.wheel, 1.02)
	local scrollSpeed = baseSpeed * multiplier

	local newY = self.content:getY() + e.wheel * scrollSpeed
	if newY > self.maxY then newY = self.maxY end
	if newY < self.minY then newY = self.minY end
	self.content:setY(newY)
	self:updateScrollbarFromContent()

	self.scrollVelocity = e.wheel * scrollSpeed
	if self.scrollVelocity > self.maxVelocity then self.scrollVelocity = self.maxVelocity end
	if self.scrollVelocity < -self.maxVelocity then self.scrollVelocity = -self.maxVelocity end
end

function ScrollableGridAI:onKeyDown(e)
	if e.keyCode == KeyCode.UP then self.isup = true
	elseif e.keyCode == KeyCode.DOWN then self.isdown = true
	end
	if e.keyCode == KeyCode.PAGE_UP then self.isqup = true
	elseif e.keyCode == KeyCode.PAGE_DOWN then self.isqdown = true
	end
end

function ScrollableGridAI:onKeyUp(e)
	if e.keyCode == KeyCode.UP then self.isup = false
	elseif e.keyCode == KeyCode.DOWN then self.isdown = false
	end
	if e.keyCode == KeyCode.PAGE_UP then self.isqup = false
	elseif e.keyCode == KeyCode.PAGE_DOWN then self.isqdown = false
	end
end

--[[
-- ENTER_FRAME: momentum + scrollbar sync
function ScrollableGridAI:onEnterFrame(e)
	if (-self.scrollVelocity<>self.scrollVelocity) > self.minVelocity and not self.isDraggingContent and not self.isDraggingScrollbar then
		local newY = self.content:getY() + self.scrollVelocity
		if newY > self.maxY then
			newY = self.maxY
			self.scrollVelocity = 0
		elseif newY < self.minY then
			newY = self.minY
			self.scrollVelocity = 0
		end
		self.content:setY(newY)
		self:updateScrollbarFromContent()
		self.scrollVelocity = self.scrollVelocity * self.friction
	elseif not self.isDraggingScrollbar then
		self:updateScrollbarFromContent()
	end
end
]]

--[[
-- Momentum update each frame with easing
function ScrollableGridAI:onEnterFrame(e)
	if (-self.scrollVelocity<>self.scrollVelocity) > self.minVelocity and not self.isDraggingContent and not self.isDraggingScrollbar then
		local newY = self.content:getY() + self.scrollVelocity

		-- Bounce / easing near edges
		if newY > self.maxY then
			-- overshoot top: push back gently
			newY = self.maxY
			self.scrollVelocity = -self.scrollVelocity * 0.5  -- 0.3, reverse with damping
		elseif newY < self.minY then
			-- overshoot bottom: push back gently
			newY = self.minY
			self.scrollVelocity = -self.scrollVelocity * 0.5 -- 0.3
		end

		self.content:setY(newY)
		self:updateScrollbarFromContent()

		-- Apply friction (slows down each frame)
		self.scrollVelocity = self.scrollVelocity * self.friction
	elseif not self.isDraggingScrollbar then
		self:updateScrollbarFromContent()
	end
end
]]

-- other flavor
-- Momentum update each frame with smooth deceleration
function ScrollableGridAI:onEnterFrame(e)
	if (-self.scrollVelocity<>self.scrollVelocity) > self.minVelocity and
		not self.isDraggingContent and not self.isDraggingScrollbar then
		local newY = self.content:getY() + self.scrollVelocity

		-- Clamp at edges, but ease out instead of bouncing
		if newY > self.maxY then
			newY = self.maxY
			self.scrollVelocity = self.scrollVelocity * 0.1  -- 0.5, dampen quickly at top
		elseif newY < self.minY then
			newY = self.minY
			self.scrollVelocity = self.scrollVelocity * 0.1  -- 0.5, dampen quickly at bottom
		end

		self.content:setY(newY)
		self:updateScrollbarFromContent()

		-- Apply friction for smooth slowdown
		self.scrollVelocity = self.scrollVelocity * self.friction
	elseif not self.isDraggingScrollbar then
		self:updateScrollbarFromContent()
	end

	-- keyboard scrolling
	if self.isup and not self.isdown then
		local baseSpeed = 1/(120*2)
		local multiplier = pow(120, 1.02)
		local scrollSpeed = baseSpeed * multiplier
		self.scrollVelocity = 100 * scrollSpeed
	elseif self.isdown and not self.isup then
		local baseSpeed = 1/(120*2)
		local multiplier = pow(120, 1.02)
		local scrollSpeed = baseSpeed * multiplier
		self.scrollVelocity = -100 * scrollSpeed
	end
	if self.isqup and not self.isqdown then
		local baseSpeed = 1/(120*2)
		local multiplier = pow(120, 1.02)
		local scrollSpeed = baseSpeed * multiplier
		self.scrollVelocity = 100*2 * scrollSpeed
	elseif self.isqdown and not self.isqup then
		local baseSpeed = 1/(120*2)
		local multiplier = pow(120, 1.02)
		local scrollSpeed = baseSpeed * multiplier
		self.scrollVelocity = -100*2 * scrollSpeed
	end
end

Example

application:setBackgroundColor(0x5555ff)

local scrollgridai = ScrollableGridAI.new(
	{
		contentheight=9*application:getContentHeight()/10,
		cellwidth=256,
		numcolumns=3,
	}
)
-- position
scrollgridai:setX(2*application:getContentWidth()/10)

-- order
stage:addChild(scrollgridai)

-- textures list
local texs = {
	Texture.new("gfx/100 (2).png"),
	Texture.new("gfx/200 (2).png"),
	Texture.new("gfx/300 (2).png"),
	Texture.new("gfx/48x48-blocks.png"),
	Texture.new("gfx/96x48-blocks.png"),
	Texture.new("gfx/96x48-bouncy.png"),
}

-- views (Sprites) for the scroll view
local views = {}
for i = 1, 100 do
	local s = Sprite.new()
	s:addChild(Bitmap.new(texs[math.random(#texs)]))
	views[i] = s
end

scrollgridai:loadData(views)