Android Code Snippet Simulating Android App Contacts behavior

Hi,

I have needed a kind of email address manager and I have added it the Android App Contacts behavior using a CustomListView, an sticky first char while scrolling.

screen.gif

Basically i am using two labels (lblStatus and lblStatus2) on top the CLV, moving these labels to simulate the behavior.

I am using a modificated version of the module CustomListView.bas, you can download the original module from here

Added this line to sv_ScrollChanged sub in CustomListView.bas

B4X:
Private Sub sv_ScrollChanged(Position As Int)
    CallSub(CallBack, EventName & "_ChangeChar") ' <-- Add this line
    If Position + sv.Height >= sv.Panel.Height - 5dip And DateTime.Now > LastReachEndEvent + 100 Then
        If SubExists(CallBack, EventName & "_ReachEnd") Then
            LastReachEndEvent = DateTime.Now
            CallSubDelayed(CallBack, EventName & "_ReachEnd")
        Else
            LastReachEndEvent = DateTime.Now + 1000 * DateTime.TicksPerDay 'disable
        End If
    End If
End Sub

Added this sub to CustomListView.bas, getted from this thread

B4X:
'Returns the top position of the Panel stored at the specified index.
Public Sub GetPanelPosition(Index As Int) 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
    top = top - sv.ScrollPosition
    Return top
End Sub

Finally this is the sub called from CustomListView.sv_ScrollChanged
mapChars is a map with Key = Each different email first char and Value = the index of that element in the CLV
The first Char label will stick to the top of the CLV until another first char label arrives and pushes it out of the way.

B4X:
Sub clvEmails_CangheChar
    'Sub called from CustomListView.sv_ScrollChanged
    For x = 0 To mapChars.Size - 1
        If clvEmails.GetPanelPosition(mapChars.GetValueAt(x)) < itemHeight Then
            If clvEmails.GetPanelPosition(mapChars.GetValueAt(x)) > 0  Then
                lblStatus.Top = clvEmails.GetPanelPosition(mapChars.GetValueAt(x))
                lblStatus2.Top = lblStatus.Top - lblStatus2.Height
                lblStatus2.Text = mapChars.GetKeyAt(x-1)
                If lblStatus2.Top > 0 Then
                    lblStatus2.Visible = False
                End If
            Else
                lblStatus.Top = 0
                lblStatus2.Visible = False
            End If
            lblStatus.Text = mapChars.GetKeyAt(x)
            lblStatus.Visible = True
            If x > 0 Then
                lblStatus2.Visible = True
            Else
                lblStatus2.Visible = False
            End If
        End If
    Next
    If clvEmails.GetPanelPosition(mapChars.GetValueAt(0)) > itemHeight Then
        lblStatus.Visible = False
        lblStatus2.Visible = False
    End If
    If lblStatus2.Visible = True And lblStatus.Top = 0 Then
        lblStatus2.Top = 0 - itemHeight
    End If
End Sub

Checked with CustomListView.bas version: 1.76

Any improvement in the code will be welcome
 

Attachments

  • appContacts.zip
    13 KB · Views: 284
Top