Android Question Horizontal scrolling panels

vecino

Well-Known Member
Licensed User
Longtime User
Hello, I need to scroll horizontally approximately square panels that are inside another panel (or a view "horizontalscrollview"?).
I would like to scroll by dragging with your finger. If you can not, then use the arrow end.
Can this be done?, You can give me some idea to do?

Thanks and regards.
veamos.png
 

vecino

Well-Known Member
Licensed User
Longtime User
Hello, I think BetterSlidingPanels only displays a panel at a time, I need to do something like this:
Is it possible?

example.png
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use HorizontalScrollView for that. See the attached example.

The code is:
B4X:
Sub Process_Globals
   Private scrollTimer As Timer
   Private lastScrollTime As Long
End Sub

Sub Globals
   Private itemSize As Int
   Private HorizontalScrollView1 As HorizontalScrollView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     scrollTimer.Initialize("scrollTimer", 50)
   End If
   Activity.LoadLayout("1")
   itemSize = HorizontalScrollView1.Width / 3
   HorizontalScrollView1.Panel.Width = itemSize * 10
   For i = 0 To 9
     Dim p As Panel
     p.Initialize("")
     p.Color = Rnd(0x80000000, -1)
     HorizontalScrollView1.Panel.AddView(p, itemSize * i, 0, itemSize, HorizontalScrollView1.Panel.Height)
   Next
End Sub


Sub Button1_Click
   HorizontalScrollView1.ScrollPosition = Max(0, HorizontalScrollView1.ScrollPosition - itemSize)
End Sub

Sub Button2_Click
   HorizontalScrollView1.ScrollPosition = Min(HorizontalScrollView1.Panel.Width, HorizontalScrollView1.ScrollPosition + itemSize)
End Sub

Sub HorizontalScrollView1_ScrollChanged(Position As Int)
   If lastScrollTime > DateTime.Now Then Return
   If scrollTimer.Enabled = False Then scrollTimer.Enabled = True
   lastScrollTime = DateTime.Now
End Sub

Sub ScrollTimer_Tick
   If lastScrollTime + 100 < DateTime.Now Then
     scrollTimer.Enabled = False
     Dim sp As Int = HorizontalScrollView1.ScrollPosition
     lastScrollTime = DateTime.Now + 100
     HorizontalScrollView1.ScrollPosition = Round(sp / itemSize) * itemSize
   End If
End Sub
 

Attachments

  • FixedScrollView.zip
    27.3 KB · Views: 388
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Thanks, it's great!
I have worked many years with other languages and I still find it difficult, but it is easier with the examples as you put.
Is there a programming book with B4A?, A book teach you to do things like that you've written.

By the way, how I can know which panel has been pressed?

Thank you very much.
 
Upvote 0

notedop

Member
Licensed User
Longtime User
When creating the panel you can put a tag in it. Then in the On click event the SENDER variable will represent the panel that has been pushed. dim a panel and assign sender to that panel in the on click event. You can then read the tag to identify it.
 
Upvote 0
Top