B4J Question [Solved] AutoShowScrollbar in CustomListView

aeric

Expert
Licensed User
Longtime User
By default, we need to set the property in designer. There is a ShowScrollBar property in PCLV.
I want to know is there any way that I can make my horizontal CLV hide the scrollbar when all the items are displayed and only show the scrollbar when the CLV is scrollable?
 
Solution
The solution from @Sagenut works fine except when you use the mouse wheel to scroll vertically.
1734350571668.png

To capture the mouse wheel event you can use the code suggested by @stevel05 in this thread: scroll event for mouse wheel
So use Reflection to disable the mouse wheel scrolling:
[CODE lang="b4x" highlight="18-20,22-27"]Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Private LblCategory As Label
Private ClvCategories As CustomListView
Private CLVHeight As Int 'This will store the original CLV Height
'Dim Size As Int = 4
' Dim Size As Int = 10
End Sub
Public Sub Initialize
' B4XPages.GetManager.LogEvents = True
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1...
1734078334188.png

You can switch off the Show Scroll Bar property of the CLV.
In the fill_clv1 subroutine you can add the lines 13 and 14.
You can change the line 5 to test.
I have used some code from my CLV_examples tutorial (page 16).
[CODE lang="b4x" highlight="5,13,14"]Private Sub fill_clv1
clv1.Clear
Dim fnames As List = get_filenames
Dim itemlst As List = get_titleitems
For i = 0 To 1 'itemlst.Size -1
Dim item As String = itemlst.Get(i)
Dim fname As String = fnames.Get(i)
Dim note As String = notesmap.Get(item)
Dim pn As Pane = set_clv1_item(i,fname,item,note)
clv1.Add(pn,itemlst.Get(i))
Hdragger.AddDragButtons
Next
Dim sv As ScrollPane = clv1.sv
sv.SetHScrollVisibility("AS_NEEDED")
End Sub
[/CODE]
The CLV uses an internal Scrollpane...
 
Upvote 0
View attachment 159467
You can switch off the Show Scroll Bar property of the CLV.
In the fill_clv1 subroutine you can add the lines 13 and 14.
You can change the line 5 to test.
I have used some code from my CLV_examples tutorial (page 16).
[CODE lang="b4x" highlight="5,13,14"]Private Sub fill_clv1
clv1.Clear
Dim fnames As List = get_filenames
Dim itemlst As List = get_titleitems
For i = 0 To 1 'itemlst.Size -1
Dim item As String = itemlst.Get(i)
Dim fname As String = fnames.Get(i)
Dim note As String = notesmap.Get(item)
Dim pn As Pane = set_clv1_item(i,fname,item,note)
clv1.Add(pn,itemlst.Get(i))
Hdragger.AddDragButtons
Next
Dim sv As ScrollPane = clv1.sv
sv.SetHScrollVisibility("AS_NEEDED")
End Sub
[/CODE]
The CLV uses an internal Scrollpane...
This works but I need to set the correct height so the clv is not vertically scrollable.
Any idea how I can set the height?
 
Upvote 0
The solution from @Sagenut works fine except when you use the mouse wheel to scroll vertically.
1734350571668.png

To capture the mouse wheel event you can use the code suggested by @stevel05 in this thread: scroll event for mouse wheel
So use Reflection to disable the mouse wheel scrolling:
[CODE lang="b4x" highlight="18-20,22-27"]Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Private LblCategory As Label
Private ClvCategories As CustomListView
Private CLVHeight As Int 'This will store the original CLV Height
'Dim Size As Int = 4
' Dim Size As Int = 10
End Sub
Public Sub Initialize
' B4XPages.GetManager.LogEvents = True
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
B4XPages.SetTitle(Me, "Main Title")
CLVHeight = ClvCategories.AsView.Height 'Store the original CLV Height
Dim R As Reflector
R.Target = ClvCategories.sv
R.AddEventFilter("ClvVscroll","javafx.scene.input.ScrollEvent.SCROLL")
End Sub
Private Sub ClvVscroll_Filter (E As Event)
' Dim MouseEvent As JavaObject = E
' Dim deltaY As Double = MouseEvent.RunMethod("getDeltaY",Null)
' Log(deltaY)
E.Consume
End Sub
Private Sub CreateButtonCategory (ButtonText As String, Height As Double) As B4XView
Dim p As B4XView = xui.CreatePanel("")
p.SetLayoutAnimated(0, 0, 0, 150dip, Height)
#If B4J
p.Color = xui.Color_Transparent
#End If
p.LoadLayout("BtnCategory")
LblCategory.Text = ButtonText
LblCategory.Alignment = "CENTER" 'no effect
Return p
End Sub
Private Sub LblCategory_MouseClicked (EventData As MouseEvent)
Dim lbl As Label = Sender
xui.MsgboxAsync(lbl.Text & " clicked.","Category click event")
End Sub
Private Sub Button1_Click
ClvCategories.Add(CreateButtonCategory("Sample " & ClvCategories.Size, CLVHeight), ClvCategories.Size)
Sleep(10)
ScrollCheck
End Sub
Private Sub Button2_Click
ClvCategories.RemoveAt(ClvCategories.Size - 1)
Sleep(10)
ScrollCheck
End Sub
Private Sub ScrollCheck
Dim CLVWidth As Int
Dim p As Pane
For x = 0 To ClvCategories.Size - 1
p = ClvCategories.GetPanel(x)
CLVWidth = CLVWidth + (p.Width)
Next
Dim sv As ScrollPane = ClvCategories.sv
If CLVWidth > ClvCategories.AsView.Width Then
ClvCategories.Base_Resize(ClvCategories.AsView.Width, ClvCategories.AsView.Height + 18dip)
sv.SetHScrollVisibility("ALWAYS")
Else
ClvCategories.Base_Resize(ClvCategories.AsView.Width, CLVHeight)
sv.SetHScrollVisibility("NEVER")
End If
End Sub
[/CODE]
It's enough to consume the ClvVscroll_Filter event to stop the mouse wheel to work on the ClvCategories.sv
1734350977164.png

Thank you @stevel05 !
 
Upvote 0
Solution
The solution from @Sagenut works fine except when you use the mouse wheel to scroll vertically.
View attachment 159552
To capture the mouse wheel event you can use the code suggested by @stevel05 in this thread: scroll event for mouse wheel
So use Reflection to disable the mouse wheel scrolling:
[CODE lang="b4x" highlight="18-20,22-27"]Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Private LblCategory As Label
Private ClvCategories As CustomListView
Private CLVHeight As Int 'This will store the original CLV Height
'Dim Size As Int = 4
' Dim Size As Int = 10
End Sub
Public Sub Initialize
' B4XPages.GetManager.LogEvents = True
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
B4XPages.SetTitle(Me, "Main Title")
CLVHeight = ClvCategories.AsView.Height 'Store the original CLV Height
Dim R As Reflector
R.Target = ClvCategories.sv
R.AddEventFilter("ClvVscroll","javafx.scene.input.ScrollEvent.SCROLL")
End Sub
Private Sub ClvVscroll_Filter (E As Event)
' Dim MouseEvent As JavaObject = E
' Dim deltaY As Double = MouseEvent.RunMethod("getDeltaY",Null)
' Log(deltaY)
E.Consume
End Sub
Private Sub CreateButtonCategory (ButtonText As String, Height As Double) As B4XView
Dim p As B4XView = xui.CreatePanel("")
p.SetLayoutAnimated(0, 0, 0, 150dip, Height)
#If B4J
p.Color = xui.Color_Transparent
#End If
p.LoadLayout("BtnCategory")
LblCategory.Text = ButtonText
LblCategory.Alignment = "CENTER" 'no effect
Return p
End Sub
Private Sub LblCategory_MouseClicked (EventData As MouseEvent)
Dim lbl As Label = Sender
xui.MsgboxAsync(lbl.Text & " clicked.","Category click event")
End Sub
Private Sub Button1_Click
ClvCategories.Add(CreateButtonCategory("Sample " & ClvCategories.Size, CLVHeight), ClvCategories.Size)
Sleep(10)
ScrollCheck
End Sub
Private Sub Button2_Click
ClvCategories.RemoveAt(ClvCategories.Size - 1)
Sleep(10)
ScrollCheck
End Sub
Private Sub ScrollCheck
Dim CLVWidth As Int
Dim p As Pane
For x = 0 To ClvCategories.Size - 1
p = ClvCategories.GetPanel(x)
CLVWidth = CLVWidth + (p.Width)
Next
Dim sv As ScrollPane = ClvCategories.sv
If CLVWidth > ClvCategories.AsView.Width Then
ClvCategories.Base_Resize(ClvCategories.AsView.Width, ClvCategories.AsView.Height + 18dip)
sv.SetHScrollVisibility("ALWAYS")
Else
ClvCategories.Base_Resize(ClvCategories.AsView.Width, CLVHeight)
sv.SetHScrollVisibility("NEVER")
End If
End Sub
[/CODE]
It's enough to consume the ClvVscroll_Filter event to stop the mouse wheel to work on the ClvCategories.sv
View attachment 159555
Thank you @stevel05 !
I just tested the code. Very nice. Maybe someone can make this a new custom view.
 
Upvote 0
Back
Top