AMD_ScrollableListItemMixin = {} -- does the visual row display as the list is scrolled
function AMD_ScrollableListItemMixin:Init(elementData)
self.Background:SetColorTexture(elementData.color:GetRGBA())
self.Text:SetText(elementData.text)
end
AMD_TabOnCharacterFrameMixin = {}
function AMD_TabOnCharacterFrameMixin:OnLoad() -- add 100 items to the list to be scrolled
for _ = 1, 100 do
self.ListFrame:AppendListItem()
end
end
AMD_ScrollableListMixin = {}
function AMD_ScrollableListMixin:OnLoad() -- initialise the list
-- The data provider acts as the backing model for a scrollview each
-- element ("elementData") in the data provider corresponds to one row
-- in the displayed list. The actual values can be anything. The default
-- below creates an empty data provider, but you can pass a table in as
-- the first argument to pre-populate it with values.
self.DataProvider = CreateDataProvider()
-- The scroll view is responsible for managing the acquisition of pooled
-- frames and laying them out for display. This needs to be supplied with
-- a data provider, an element extent (size), and an element factory or
-- initializer at minimum.
--
-- The extent must be the actual height of each row. It can be dynamically
-- calculated with a function, but a fixed extent is much more performant.
local elementExtent = 21
self.ScrollView = CreateScrollBoxListLinearView()
self.ScrollView:SetDataProvider(self.DataProvider)
self.ScrollView:SetElementExtent(elementExtent)
--[[ -- Currently use on Wrath pre 3.4.1 and Vanilla
self.ScrollView:SetElementInitializer("Frame", "AMD_ScrollableListItemTemplate", function(frame, elementData)
-- This is called each time the scrollview acquires a frame this
-- should generally call a method on the acquired frame and update
-- its visual state accordingly.
frame:Init(elementData)
end)
]]--
--[[ Use on Retail and Wrath post 3.4.1 ]]--
self.ScrollView:SetElementInitializer("AMD_ScrollableListItemTemplate", function(frame, elementData)
-- This is called each time the scrollview acquires a frame this
-- should generally call a method on the acquired frame and update
-- its visual state accordingly.
frame:Init(elementData)
end)
-- Padding and spacing are optional these are just demo values. The
-- defaults if no padding is configured will all be zero.
local paddingT = 10
local paddingB = 10
local paddingL = 10
local paddingR = 10
local spacing = 5
self.ScrollView:SetPadding(paddingT, paddingB, paddingL, paddingR, spacing)
-- The below call is required to hook everything up automatically.
ScrollUtil.InitScrollBoxListWithScrollBar(self.ScrollBox, self.ScrollBar, self.ScrollView)
-- If you want the scrollbar to show or hide automatically based on
-- whether or not the region can be scrolled, you'll need two anchor
-- tables to configure the managed scroll visibility behavior.
local anchorsWithBar = {
CreateAnchor("TOPLEFT", self, "TOPLEFT", 4, -4),
CreateAnchor("BOTTOMRIGHT", self.ScrollBar, "BOTTOMLEFT", 0, 4),
}
local anchorsWithoutBar = {
CreateAnchor("TOPLEFT", self, "TOPLEFT", 4, -4),
CreateAnchor("BOTTOMRIGHT", self, "BOTTOMRIGHT", -4, 4),
}
ScrollUtil.AddManagedScrollBarVisibilityBehavior(self.ScrollBox, self.ScrollBar, anchorsWithBar, anchorsWithoutBar)
end
function AMD_ScrollableListMixin:AppendListItem() -- this creates the line items
local color = CreateColor(fastrandom(), fastrandom(), fastrandom())
local text = string.format("Time: %.5f", GetTime())
local elementData = -- Used by the list to display each list row text and color as they're scrolled
{
color = color,
text = text,
}
self.DataProvider:Insert(elementData)
self.ScrollBox:ScrollToEnd(ScrollBoxConstants.NoScrollInterpolation)
end
function AMD_ScrollableListMixin:RemoveListItem()
local lastIndex = self.DataProvider:GetSize()
self.DataProvider:RemoveIndex(lastIndex)
end