注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

沉默蜂B4A安卓编程

国内资料最全的B4A资料库

 
 
 

日志

 
 

[原创]高级迷你菜单(二)  

2014-10-17 15:23:21|  分类: 菜单控件 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
下面是类模块CustomListView代码



'来源 [Class] CustomListView - A flexible List based on ScrollView

'version: 1.10
#Event: ItemClick (Index As Int, Value As Object)

Sub Class_Globals

Private sv As ScrollView
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
End Sub


Public Sub Initialize (vCallback As Object, vEventName As String)

EventName = vEventName
CallBack = vCallback

sv.Initialize2(0, "sv")
'彻底隐藏滚动条,沉默蜂为了应对菜单而特地增加的一个功能
' Disable ScrollView overscroll effect
' http://www.basic4ppc.com/android/forum/threads/disable-ScrollView-overscroll-effect.27466/#post-159201
Dim r As Reflector
r.Target = sv
r.RunMethod2("setVerticalScrollBarEnabled", False, "java.lang.boolean")
r.RunMethod2("setOverScrollMode", 2, "java.lang.int" ) ' 0 = OVER_SCROLL_ALWAYS, 1 = OVER_SCROLL_IF_CONTENT_SCROLLS, 2 = OVER_SCROLL_NEVER

items.Initialize
panels.Initialize
'项目分隔线高度
dividerHeight = 1dip
'分隔线颜色
' sv.Color = 0xFFD9D7DE 'this sets the dividers color
sv.Color=Colors.ARGB(15,210,210,210)
Dim r As Reflector
Dim idPressed As Int
   idPressed = r.GetStaticField("android.R$drawable", "list_selector_background")
    r.Target = r.GetContext
    r.Target = r.RunMethod("getResources")
pressedDrawable = r.RunMethod2("getDrawable", idPressed, "java.lang.int")
'项目文本的颜色
DefaultTextColor = Colors.Black
'项目文本字体大小
DefaultTextSize = 16
'面板背景色
' DefaultTextBackgroundColor = Colors.Red
DefaultTextBackgroundColor = Colors.White
DefaultTextBackground = Null
End Sub

'Returns a view object that holds the list.
Public Sub AsView As View
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

Public Sub SetVisible(v As Boolean)
sv.Visible=v
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.Color=Colors.White
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 gdwPressed As GradientDrawable
'  gdwPressed.Initialize("BOTTOM_TOP",Array As Int(Colors.Red,Colors.Red))  
'  gdwPressed.CornerRadius = 7   ' Define a GradientDrawable for Disabled state 
'
' Dim sd As StateListDrawable
'    sd.Initialize
'  sd.AddState(sd.State_Pressed, gdwPressed)
'    sd.AddCatchAllState(Pnl.Background)
' 采用默认的按下颜色
Dim sd As StateListDrawable
    sd.Initialize
    sd.AddState(sd.State_Pressed, pressedDrawable)
    sd.AddCatchAllState(Pnl.Background)
'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
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 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

'跳到列表最顶部,沉默蜂编写
Public Sub JumpToTop
sv.ScrollPosition = 0
End Sub

'跳到列表最底部,沉默蜂编写
Public Sub JumpToBottom
sv.ScrollPosition = sv.Height
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

  评论这张
 
阅读(239)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017