Android Question CLV change item color when reach end

hl88

Member
Hello everyone. I have a question
In Messenger. chat bubble slowly change when user scroll down and up
Facebook-Messenger-4-color-gradients-840x578.jpg


I want do that with Customlistview. top panel holding text content is purple and bottom is pink
B4X:
Sub CLV1_ReachEnd
    'Some function
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1603695096240.png


B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private CustomListView1 As CustomListView
End Sub

Public Sub Initialize
    
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    For i = 1 To 100
        CustomListView1.Add(CreateItem, "")
    Next
End Sub

Private Sub CreateItem As B4XView
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, CustomListView1.AsView.Width, Rnd(40dip, 100dip))
    Dim child As B4XView = xui.CreatePanel("")
    p.AddView(child, 10dip, 2dip, Rnd(p.Width / 2, p.Width - 20dip), p.Height - 4dip)
    child.SetColorAndBorder(0, 1dip, 0, 20dip)
    Return p
End Sub

Private Sub CustomListView1_VisibleRangeChanged (FirstIndex As Int, LastIndex As Int)
    For i = FirstIndex To LastIndex
        Dim raw As CLVItem = CustomListView1.GetRawListItem(i)
        Dim pnl As B4XView = raw.Panel.GetView(0) 
        pnl.GetView(0).Color = PositionToColor(raw.Offset - CustomListView1.sv.ScrollViewOffsetY)
    Next
End Sub

Private Sub PositionToColor(offset As Int) As Int
    Dim StartR As Int = 0xFF
    Dim StartG As Int = 0x00
    Dim StartB As Int = 0xD6
    Dim EndR As Int = 0x00
    Dim EndG As Int = 0x00
    Dim EndB As Int = 0xFF
    
    Dim f As Float = Max(0, Min(1, offset / CustomListView1.sv.Height))
    Dim r As Int = Bit.And(0xff, StartR + (EndR - StartR) * f)
    Dim g As Int = Bit.And(0xff, StartG + (EndG - StartG) * f)
    Dim b As Int = Bit.And(0xff, StartB + (EndB - StartB) * f)
    Return xui.Color_ARGB(255, r, g, b)
End Sub
 
Upvote 0

hl88

Member
Wow, amazing, thanks you @Erel
I have to modify the sub CreateItem in order to add my panel layout into CLV. I want each item has fixed height

Private Sub B4XPage_Created (Root1 As B4XView)
B4X:
AddCLVitem(Item, Link)

Sub AddCLVItem(Title As String, Link As String)
B4X:
Dim p As B4XView = xui.CreatePanel("")
    p.LoadLayout("lyItem")
    lbTitle.Text = Title   
    CustomListView1.Add(p,Link)
 
Upvote 0
Top