I have a array of cities –
String[] cities = { "Riyadh", "Jubail", "Jeddah", "Madinah",
"Tabuk","Bangalore","Chennai","Pune" };
I am displaying them in a list that has three textview(fixed) .
This means we are going to display three cities at a time.
Here is the method to display the cities.
void setUpCityList(int iCity) {
tvOneCity.setText(cities[iCity]);
tvTwoCity.setText(cities[iCity + 1]);
tvThreeCity.setText(cities[iCity + 2]);
}
When the user swipes up ,i am incrementing the index and the user swipes down i am decrementing the index.
void swipeUp()
{
iCity++;
setUpCityList(iCity);
}
void swipeDown()
{
iCity--;
setUpCityList(iCity);
}
I want to give this list a endless scroll functionality .
That means if the user swipes up – and list reaches the last three elements , it should again start from first element.
Example –
1. Current display – Last three elements – “Bangalore”,”Chennai”,”Pune”
2. Swipe up — Display should be – Chennai , Pune , Riyadh
How can i achieve this functionality .
This is more of a programming question .
Any Help will be highly appreciated.
You just need to change the indexing in your method:
Once the
iCityoriCity + 1, etc, reaches thelength, actual index will automatically become0. And for values less than length, it will behave normally, as if there was nomodulusoperator.This way you can create a cycle of as much item you want. So, it’s a generalized and extensible solution.