Thursday, April 13, 2017

A Simple Scroll Function


Imagine you want to display a long list of items but you have a limited area to show them on screen. In that case, you have to resort to using scroll.  Let me explain in pseudo-code one way of doing it.

Length is a function that gives the number of items contained in an array of items.
In our example DisplayLength will tell us how many items will be shown on screen. Here, we have set its value to two.

DisplayLength = 2;

Index will be the variable that holds the pointer of the current item on the list. At the beginning, it will point to the first item on the list. Hence index=0;
(Note: Computer scientists always like to start counting at 0)

Index = 0; 

Scroll_Limit will tell us how far we can keep scrolling. When Index = Scroll-Limit that means that we have reached the end of our scroll capabilities and that all items have been displayed to the user.
This is how to calculate it.

Scroll_Limit = Length (List A) - DisplayLength

In our example. Scroll_Limit = (6 - 2) = 4.

Here is our list: 

LIST A   (6 items)                DISPLAY (2 items)

aaaaa 0  -> index points to first item;
bbbbb 1
ccccc 2    
ddddd 3  
eeeee 4    
fffff 5

Imagine that you want to go forward in your list by pressing the arrow down button  (while index <= scroll_Limit). Remember that in this example, Scroll_Limit=4. This would be the result.

Now to go back, say, with the press of the up arrow key. You just have to decrease the value of index.
index-=1; Remember to set the limit at 0. Allow the key to be processed only if index>0;

Output:




No comments:

Post a Comment