iOS Question CustomListView ScrollToItem(index) help

Rory Mapstone

Member
Licensed User
Longtime User
I found the following related question: https://www.b4x.com/android/forum/threads/customlistview-scrolltoitem-problem.90996/#post-576490

What I want is to scroll to the last item in the custom listview.

Using xCustomListView v1.54 library

B4X:
Sub scrollToLast
'Scroll to the end of the list only once
    If ScrollToEnd Then
        If clv_comments.LastVisibleIndex <  clv_comments.getsize-1 Then
            Sleep(1000) 'put this in as a test
            clv_comments.ScrollToItem(clv_comments.GetSize-1) 'this seems to JUMP to the item and puts the last item at the top of the screen making all previous comments not visible.
            ScrollToEnd = False
        End If
    End If
End Sub

The behavior I am seeing is that the list "jumps" to the item and does not scroll. Additionally the item is placed at the top of the screen, would it be possible to get the item to be at the bottom of the screen.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Change ScrollToItem to:
B4X:
Public Sub ScrollToItem(Index As Int)
   Dim offset As Float = FindItemOffset(Index)
   #if B4i
   Dim nsv As ScrollView = sv
   If horizontal Then
       nsv.ScrollTo(offset, 0, True)
   Else
       nsv.ScrollTo(0, offset, True)
   End If
   #else if B4J
   JumpToItem(Index)
   #Else If B4A
   
   If horizontal Then
       Dim hv As HorizontalScrollView = sv
       hv.ScrollPosition = offset 'smooth scroll
   Else
       Dim nsv As ScrollView = sv
       nsv.ScrollPosition = offset
   End If
   #End If
End Sub
 
Upvote 0

Rory Mapstone

Member
Licensed User
Longtime User
Thank you, so simple. I was trying to solve the issue by using the calculated content height and screen height :(.

The offset for the last item is quite high, it ends up on the top of the screen. Would I need to calculate screen or content height to get the last item anchored to the bottom ?


Current Result:


Desired Reuslt:
 
Last edited:
Upvote 0

Rory Mapstone

Member
Licensed User
Longtime User
I changed the offset as follows, seems to be correct:

B4X:
Dim offset As Float = FindItemOffset(Index)
    offset = offset - sv.Height + GetItem(Index).Panel.Height
    If offset < 0 Then offset = 0
 
Last edited:
Upvote 0
Top