Android Question Job Release on multiple keypress up or down on remote control

ronovar

Active Member
Licensed User
Longtime User
I have ListView that holds 500 items...

Item1
Item2
Item3
Item4
.
.
.
Item497
Item498
Item499
Item500

When i scroll down by one i call Job Download whitch downloads image from server...this works excellent if i scroll by one with small delay between item scroll down or up...if i scroll fast down 3x times i get image from 1item, 2item and 3item..how i get only 3 item?

Example:

I im at Item1, a press on remote control to scroll down fast 3 times button down so i get at Item4, on imageview i see then item1 image, then item2 image, item3 image and finally item4 image...how could i get image 4 display?

So that i display from item1 image to item4 image? To not to download images between?

I im using this code:

B4X:
'GET - KeyUp
Sub Activity_KeyUp (KeyCode As Int) As Boolean
    'SELECT - Remote Code
    Select KeyCode
        Case KeyCodes.KEYCODE_DPAD_DOWN
            'UPDATE - Movies
            UpdateMovies(ScrolledMovie)
               
            'GET - MovieCover
            GetMovieCover(Movies(ScrolledMovie, 14))
        Case KeyCodes.KEYCODE_DPAD_UP
            'UPDATE - Movies
            UpdateMovies(ScrolledMovie)
               
            'GET - MovieCover
            GetMovieCover(Movies(ScrolledMovie, 14))
        Case 92
            'UPDATE - Movies
            UpdateMovies(ScrolledMovie)
               
            'GET - MovieCover
            GetMovieCover(Movies(ScrolledMovie, 14))
        Case 93
            'UPDATE - Movies
            UpdateMovies(ScrolledMovie)
               
            'GET - MovieCover
            GetMovieCover(Movies(ScrolledMovie, 14))
    End Select
End Sub

'GET - MovieCover
Sub GetMovieCover(CoverID As Int)
    'DEFINE - Jobs
    Dim Job0 As HttpJob

    'INITIALIZE - Job0
    Job0.Initialize("Job0", Me)

    'GET - MovieCover
    Job0.Download("http://myurl/moviecoverID.jpg")
End Sub

'DONE - Jobs
Sub JobDone(Job As HttpJob)
    'DEFINE - RSImageEffects
    Dim RSImageEffects1 As RSImageEffects

    'CHECK - Jobs
    If Job.Success = True Then
        Select Job.JobName
            Case "Job0"
                'CHECK - Return EPGs
                If (Job.GetBitmap.Width > 0) Then
                    'CENTER - Bitmaps
                    imgPreview.Gravity = Gravity.FILL

                    'SET - Bitmaps
                    imgPreview.SetBackgroundImage(Job.GetBitmap)
                   
                    'ROUND - Bitmaps
                    imgPreview.Bitmap = RSImageEffects1.RoundCorner(Job.GetBitmap, 5)
                End If
        End Select
    End If
    'RELEASE - Jobs
    Job.Release
End Sub
 

KMatle

Expert
Licensed User
Longtime User
Use a timer (or better service) which runs every 1-x seconds. Check the buttons pressed and just store it in ONE global variable and save the actual time (with miliseconds) to another (good name would be "LastKeyPressAt" or so).

When the service starts (or the timer) check how many time has passed (compare now() with the stored variable). If less than e.g. 0,5 seconds (=500 ticks) do nothing (expecting that the user isn't ready yet). At the next start, more time has passed. If another button was pressed, it's again less than 0,5 seconds between it (remember: you store the time every time a button was pressed). If it's greater, call "GetMovieCover".

It's just like on a regular tv remote typing the channel. It waits 'till the user doesn't type anymore and then it changes the channel.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
You can do this on your own, just expand your code a bit:

1. Key pressed (store key and DateTime.Now) in a public variable, increase/decrease the index, start the timer (Enabled=true) & do nothing else
2. Timer_Tick is called after that time
3. Check if DateTime.Now > stored ticks when key was pressed + 2000 (means last click was at least 2 senconds ago = user does not press anything anymore)
4. If no: Do nothing = user is still scrolling (but timer ticks in 2 seconds again)
5. If yes: Stop the timer (Enabled=False) and show the image
 
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
Soory...i was doing other stuffs in android now i create coude using your idea:

B4X:
'TICK - tmr
Sub tmr_Tick
    'CHECK - StoredTime
    If (DateTime.Now > StoredTime + 1000) Then
        'STOP - tmr
        tmr.Enabled = False
        'SHOW - image
        Log("Showing Index Image..."&StoredIndex)
    End If
End Sub

'GET - KeyPressed
Sub Activity_KeyUp (KeyCode As Int) As Boolean
    'SELECT - Remote Code
    Select KeyCode
        Case KeyCodes.KEYCODE_DPAD_DOWN
            'STORE - StoredIndex
            StoredIndex = ScrolledChannel
           
            'STORE - StoredTime
            StoredTime = DateTime.Now
           
            'START - Timer
            tmr.Enabled = True
        Case KeyCodes.KEYCODE_DPAD_UP
            'STORE - StoredIndex
            StoredIndex = ScrolledChannel
           
            'STORE - StoredTime
            StoredTime = DateTime.Now
           
            'START - Timer
            tmr.Enabled = True
    End Select
End Sub

It is working fantastics...i just change from 2seconds to 1seconds and all works as expected
 
Upvote 0
Top