Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.ListActivity;
- import android.os.Bundle;
- import android.os.CountDownTimer;
- import android.widget.AbsListView;
- import android.widget.AbsListView.OnScrollListener;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- public class MainActivity extends ListActivity {
- private String[] data = { "Abbaye de Belloc", "Abbaye du Mont des Cats",
- "Abertam", "Abondance", "Ackawi", "Acorn", "Adelost",
- "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale",
- "Aisy Cendre", "Allgauer Emmentaler", "Alverca", "Ambert",
- "American Cheese", "Ami du Chambertin", "Anejo Enchilado",
- "Anneau du Vic-Bilh", "Anthoriro", "Appenzell", "Aragon",
- "Ardi Gasna", "Ardrahan", "Armenian String", "Button (Innes)",
- "Buxton Blue", "Cabecou", "Caboc", "Cabrales", "Cachaille",
- "Caciocavallo", "Caciotta", "Caerphilly", "Cairnsmore",
- "Calenzana", "Cambazola", "Camembert de Normandie",
- "Canadian Cheddar", "Canestrato", "Cantal", "Caprice des Dieux",
- "Capricorn Goat", "Capriole Banon", "Carre de l'Est",
- "Casciotta di Urbino", "Cashel Blue", "Castellano" };
- // storing already scrolled amount of px.
- int alreadyScrolledHeight = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // nuber of rows just for easyer debugging
- for (int i = 0; i < data.length; i++) {
- data[i] = i + ". " + data[i];
- }
- setListAdapter(new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, data));
- final CountDownTimer autoscrollTimer = startAutoscroll(getListView());
- // stop autoscroll at the end of list
- getListView().setOnScrollListener(new OnScrollListener() {
- @Override
- public void onScrollStateChanged(AbsListView view, int scrollState) {
- }
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem,
- int visibleItemCount, int totalItemCount) {
- if (firstVisibleItem + visibleItemCount == totalItemCount) {
- stopAutoscroll(autoscrollTimer);
- }
- }
- });
- }
- private void stopAutoscroll(CountDownTimer autoscrollTimer) {
- autoscrollTimer.cancel();
- // this will reset scrolled height
- // if you want to pause this - just don't reset this field;
- alreadyScrolledHeight = 0;
- }
- private CountDownTimer startAutoscroll(ListView listView) {
- final long totalScrollTime = 5000;
- final int scrollPeriod = 20;
- final int heightToScroll = 5;
- final CountDownTimer timer = new CountDownTimer(totalScrollTime,
- scrollPeriod) {
- private int height;
- public void onTick(long millisUntilFinished) {
- height = (int) ((totalScrollTime - millisUntilFinished)
- / scrollPeriod * heightToScroll)
- + alreadyScrolledHeight;
- getListView().setSelectionFromTop(
- getListView().getFirstVisiblePosition(), -height);
- }
- public void onFinish() {
- // saving scroled height, and starting this timer again
- alreadyScrolledHeight = height;
- this.start();
- }
- };
- getListView().post(new Runnable() {
- @Override
- public void run() {
- timer.start();
- }
- });
- return timer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement