B4A=true Group=Default Group ModulesStructureVersion=1 Type=Class Version=7.01 @EndOfDesignText@ #Event: ItemClick (Index As Int, Value As Object) Sub Class_Globals Private sv As ScrollView2D Private items As List Private panels As List Private dividerHeight As Float 'Private pressedDrawable As Object Private EventName As String Private CallBack As Object Private su As StringUtils Public DefaultTextSize As Int Public DefaultTextColor As Int Public DefaultTextBackgroundColor As Int Private DefaultTextBackground As Object Type SortType(FileName As String, DateTaken As String, BasePanel As Panel) End Sub Public Sub Initialize (vCallback As Object, vEventName As String) 'sv.Initialize2(0, "sv") sv.Initialize(100%x,10,"sv") items.Initialize panels.Initialize dividerHeight = 0 '1dip EventName = vEventName CallBack = vCallback sv.Color = 0xFFffffff DefaultTextColor =0xFF333333 'Colors.White DefaultTextSize = 16 DefaultTextBackgroundColor = 0xFFf2f4f5 'Colors.Black DefaultTextBackground = Null sv.Panel.Height = dividerHeight End Sub 'Returns a view object that holds the list. Public Sub AsView As ScrollView2D Return sv End Sub Public Sub DesignerCreateView(base As Panel, lbl As Label, props As Map) Dim parent As Panel Dim r As Reflector r.Target = base parent = r.RunMethod("getParent") base.RemoveView 'remove the base panel parent.AddView(sv, base.Left, base.Top, base.Width, base.Height) DefaultTextSize = lbl.TextSize DefaultTextColor = lbl.TextColor DefaultTextBackground = base.Background End Sub 'Clears all items. Public Sub Clear items.Clear panels.Clear sv.Panel.Height = 0 For i = sv.Panel.NumberOfViews - 1 To 0 Step -1 sv.Panel.RemoveViewAt(i) Next End Sub 'Returns the number of items. Public Sub GetSize As Int Return items.Size End Sub 'Returns the Panel stored at the specified index. Public Sub GetPanel(Index As Int) As Panel Dim p As Panel p = panels.Get(Index) 'this is the parent panel Return p.GetView(0) End Sub 'Returns the value stored at the specified index. Public Sub GetValue(Index As Int) As Object Return items.Get(Index) End Sub 'Removes the item at the specified index. Public Sub RemoveAt(Index As Int) Dim removePanel, p As Panel removePanel = panels.Get(Index) For i = Index + 1 To items.Size - 1 p = panels.Get(i) p.Tag = i - 1 p.Top = p.Top - removePanel.Height - dividerHeight Next sv.Panel.Height = sv.Panel.Height - removePanel.Height - dividerHeight items.RemoveAt(Index) panels.RemoveAt(Index) removePanel.RemoveView End Sub 'Adds a text item. The item height will be adjusted based on the text. Public Sub AddTextItem(Text As String, Value As Object) InsertAtTextItem(items.Size, Text, Value) End Sub 'Inserts a text item at the specified index. Public Sub InsertAtTextItem(Index As Int, Text As String, Value As Object) Dim pnl As Panel pnl.Initialize("") Dim lbl As Label lbl.Initialize("") lbl.Gravity = Bit.Or(Gravity.CENTER_VERTICAL, Gravity.LEFT) pnl.AddView(lbl, 5dip, 2dip, sv.Width - 5dip, 20dip) lbl.Text = Text lbl.TextSize = DefaultTextSize lbl.TextColor = DefaultTextColor ' If DefaultTextBackground <> Null Then ' pnl.Background = DefaultTextBackground ' Else pnl.Color = DefaultTextBackgroundColor ' End If Dim minHeight As Int minHeight = su.MeasureMultilineTextHeight(lbl, Text) lbl.Height = Max(50dip, minHeight) InsertAt(Index, pnl, lbl.Height + 2dip, Value) End Sub 'Adds a custom item at the specified index. Public Sub InsertAt(Index As Int, Pnl As Panel, ItemHeight As Int, Value As Object) Dim itemPressedCololr As ColorDrawable Dim itemBkgCololr As ColorDrawable itemPressedCololr.Initialize(0xFFd9dbdc,0) itemBkgCololr.Initialize(0xFFffffff,0) Dim sd As StateListDrawable sd.Initialize sd.AddState(sd.State_Pressed, itemPressedCololr) sd.AddCatchAllState(itemBkgCololr) 'create another panel to handle the click event Dim p As Panel p.Initialize("panel") p.Background = sd Dim cd As ColorDrawable cd.Initialize(Colors.Transparent, 0) Pnl.Background = cd p.AddView(Pnl, 0, 0, sv.Width, ItemHeight) p.Tag = Index If Index = items.Size Then items.Add(Value) panels.Add(p) Dim top As Int If Index = 0 Then top = dividerHeight Else top = sv.Panel.Height End If sv.Panel.AddView(p, 0, top, sv.Width, ItemHeight) Else Dim top As Int If Index = 0 Then top = dividerHeight Else Dim previousPanel As Panel previousPanel = panels.Get(Index - 1) top = previousPanel.top + previousPanel.Height + dividerHeight End If Dim p2 As Panel For i = Index To panels.Size - 1 p2 = panels.Get(i) p2.top = p2.top + ItemHeight + dividerHeight p2.Tag = i + 1 Next items.InsertAt(Index, Value) panels.InsertAt(Index, p) sv.Panel.AddView(p, 0, top, sv.Width, ItemHeight) End If sv.Panel.Height = sv.Panel.Height + ItemHeight + dividerHeight If items.Size = 1 Then 'sv.Panel.Height = sv.Panel.Height + dividerHeight End If End Sub 'Adds a custom item. Public Sub Add(Pnl As Panel, ItemHeight As Int, Value As Object) InsertAt(items.Size, Pnl, ItemHeight, Value) End Sub 'Scrolls the list to the specified item. Public Sub JumpToItem(Index As Int) Dim top As Int Dim p As Panel For i = 0 To Min(Index - 1, items.Size - 1) p = panels.Get(i) top = top + p.Height + dividerHeight Next 'sv.ScrollPosition = top 'The scroll position doesn't always gets updated without two calls to DoEvents DoEvents 'sv.ScrollPosition = top DoEvents End Sub Private Sub Panel_Click If SubExists(CallBack, EventName & "_ItemClick") Then Dim v As View v = Sender CallSub3(CallBack, EventName & "_ItemClick", v.Tag, items.Get(v.Tag)) End If End Sub 'Returns the index of the item that holds the given view. Public Sub GetItemFromView(v As View) As Int Dim r As Reflector Dim parent, current As Object parent = v Do While (parent Is Panel) = False Or sv.Panel <> parent current = parent r.Target = current parent = r.RunMethod("getParent") Loop v = current Return v.Tag End Sub 'Public Sub MovePanelUp(Index As Int) ' '与上面的panel交换位置和tag标志 (即index) ' ' If Index = 0 Then Return ' ' Dim p1 As Panel ' Dim p2 As Panel ' ' Dim tempTop As Int ' Dim tempTag As Int ' ' p1 = panels.Get(Index-1) 'this is the parent panel ' p2 = panels.Get(Index) 'this is the parent panel ' ' tempTop = p1.Top ' tempTag = p1.Tag ' ' p1.Top = p2.Top ' p2.Top = tempTop ' ' p1.Tag = p2.Tag ' p2.Tag = tempTag ' ' ' panels.Set(Index-1,p2) ' panels.Set(Index,p1) ' 'End Sub ' 'Public Sub MovePanelDown(Index As Int) ' '与下面的panel交换位置和tag标志 (即index) ' ' If Index = panels.Size-1 Then Return ' ' Dim p1 As Panel ' Dim p2 As Panel ' ' Dim tempTop As Int ' Dim tempTag As Int ' ' p1 = panels.Get(Index) 'this is the parent panel ' p2 = panels.Get(Index+1) 'this is the parent panel ' ' tempTop = p1.Top ' tempTag = p1.Tag ' ' p1.Top = p2.Top ' p2.Top = tempTop ' ' p1.Tag = p2.Tag ' p2.Tag = tempTag ' ' ' panels.Set(Index,p2) ' panels.Set(Index+1,p1) ' 'End Sub public Sub SortList(SortType As Int) Dim i As Int Dim ListTop As List Dim ListSort As List ListTop.Initialize ListSort.Initialize For i=0 To panels.Size - 1 Dim BasePanel As Panel Dim PanelOnBasePanel As Panel BasePanel = panels.Get(i) ListTop.Add(BasePanel.Top) PanelOnBasePanel = BasePanel.GetView(0) Dim labelFileName As Label Dim labelDateTaken As Label labelFileName = PanelOnBasePanel.GetView(1) labelDateTaken = PanelOnBasePanel.GetView(3) Dim SortData As SortType SortData.Initialize SortData.FileName = labelFileName.Text SortData.BasePanel = BasePanel SortData.DateTaken = labelDateTaken.Text 'Log(SortData.FileName & " - " & BasePanel.Tag & " - " & DateTime.Date(SortData.DateTaken) & " " & DateTime.Time(SortData.DateTaken) ) ListSort.Add(SortData) Next Select Case SortType Case 0 ListSort.SortType("FileName",True) Case 1 ListSort.SortType("FileName",False) Case 2 ListSort.SortType("DateTaken",True) Case 3 ListSort.SortType("DateTaken",False) End Select 'Log("----------") panels.Clear For i = 0 To ListSort.Size - 1 Dim BasePanel2 As Panel Dim SortData2 As SortType SortData2 = ListSort.Get(i) BasePanel2 = SortData2.BasePanel 'Log(SortData2.Name & " - " & BasePanel2.Tag) BasePanel2.Tag = i BasePanel2.Top = ListTop.Get(i) panels.Add(BasePanel2) Next ListTop.Clear ListSort.Clear End Sub Public Sub DragPanelto(IndexSource As Int, IndexTarget As Int) If IndexSource < 0 Then IndexSource = 0 If IndexTarget < 0 Then IndexTarget = 0 If IndexSource > panels.Size - 1 Then IndexSource = panels.Size - 1 If IndexTarget > panels.Size - 1 Then IndexTarget = panels.Size - 1 If IndexSource = IndexTarget Then Return Dim i As Int Dim ListTop As List ListTop.Initialize Dim ListSort As List ListSort.Initialize For i=0 To panels.Size - 1 Dim BasePanel As Panel BasePanel = panels.Get(i) ListTop.Add(BasePanel.Top) ListSort.Add(BasePanel) Next Dim SourcePanel As Panel If IndexSource < IndexTarget Then SourcePanel = panels.Get(IndexSource) 'SourcePanel.Top = ListTop.Get(IndexTarget) SourcePanel.SetLayoutAnimated(400,SourcePanel.Left,ListTop.Get(IndexTarget),SourcePanel.Width,SourcePanel.Height) SourcePanel.Tag = IndexTarget For i = IndexSource + 1 To IndexTarget Dim MovePanel As Panel MovePanel = panels.Get(i) 'MovePanel.Top = ListTop.Get(i-1) MovePanel.SetLayoutAnimated(400,MovePanel.Left,ListTop.Get(i-1),MovePanel.Width,MovePanel.Height) MovePanel.Tag = i-1 Next Else SourcePanel = panels.Get(IndexSource) 'SourcePanel.Top = ListTop.Get(IndexTarget) SourcePanel.SetLayoutAnimated(400,SourcePanel.Left,ListTop.Get(IndexTarget),SourcePanel.Width,SourcePanel.Height) SourcePanel.Tag = IndexTarget For i = IndexTarget To IndexSource - 1 Dim MovePanel As Panel MovePanel = panels.Get(i) 'MovePanel.Top = ListTop.Get(i+1) MovePanel.SetLayoutAnimated(400,MovePanel.Left,ListTop.Get(i+1),MovePanel.Width,MovePanel.Height) MovePanel.Tag = i+1 Next End If panels.Clear Dim k As Int For k = 0 To ListSort.Size-1 For i=0 To ListSort.Size - 1 Dim tmpPan As Panel tmpPan = ListSort.Get(i) If tmpPan.Tag = k Then panels.Add(tmpPan) Exit End If Next Next ListTop.Clear ListSort.Clear End Sub