Advertisement
Guest User

Untitled

a guest
Dec 31st, 2012
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. import android.app.ListActivity;
  2. import android.os.Bundle;
  3. import android.os.CountDownTimer;
  4. import android.widget.AbsListView;
  5. import android.widget.AbsListView.OnScrollListener;
  6. import android.widget.ArrayAdapter;
  7. import android.widget.ListView;
  8.  
  9. public class MainActivity extends ListActivity {
  10.  
  11.     private String[] data = { "Abbaye de Belloc", "Abbaye du Mont des Cats",
  12.             "Abertam", "Abondance", "Ackawi", "Acorn", "Adelost",
  13.             "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale",
  14.             "Aisy Cendre", "Allgauer Emmentaler", "Alverca", "Ambert",
  15.             "American Cheese", "Ami du Chambertin", "Anejo Enchilado",
  16.             "Anneau du Vic-Bilh", "Anthoriro", "Appenzell", "Aragon",
  17.             "Ardi Gasna", "Ardrahan", "Armenian String", "Button (Innes)",
  18.             "Buxton Blue", "Cabecou", "Caboc", "Cabrales", "Cachaille",
  19.             "Caciocavallo", "Caciotta", "Caerphilly", "Cairnsmore",
  20.             "Calenzana", "Cambazola", "Camembert de Normandie",
  21.             "Canadian Cheddar", "Canestrato", "Cantal", "Caprice des Dieux",
  22.             "Capricorn Goat", "Capriole Banon", "Carre de l'Est",
  23.             "Casciotta di Urbino", "Cashel Blue", "Castellano" };
  24.  
  25.     // storing already scrolled amount of px.
  26.     int alreadyScrolledHeight = 0;
  27.  
  28.     @Override
  29.     protected void onCreate(Bundle savedInstanceState) {
  30.         super.onCreate(savedInstanceState);
  31.         // nuber of rows just for easyer debugging
  32.         for (int i = 0; i < data.length; i++) {
  33.             data[i] = i + ". " + data[i];
  34.         }
  35.  
  36.         setListAdapter(new ArrayAdapter<String>(this,
  37.                 android.R.layout.simple_list_item_1, data));
  38.  
  39.         final CountDownTimer autoscrollTimer = startAutoscroll(getListView());
  40.  
  41.         // stop autoscroll at the end of list
  42.         getListView().setOnScrollListener(new OnScrollListener() {
  43.             @Override
  44.             public void onScrollStateChanged(AbsListView view, int scrollState) {
  45.             }
  46.  
  47.             @Override
  48.             public void onScroll(AbsListView view, int firstVisibleItem,
  49.                     int visibleItemCount, int totalItemCount) {
  50.                 if (firstVisibleItem + visibleItemCount == totalItemCount) {
  51.                     stopAutoscroll(autoscrollTimer);
  52.                 }
  53.             }
  54.         });
  55.     }
  56.  
  57.     private void stopAutoscroll(CountDownTimer autoscrollTimer) {
  58.         autoscrollTimer.cancel();
  59.         // this will reset scrolled height
  60.         // if you want to pause this - just don't reset this field;
  61.         alreadyScrolledHeight = 0;
  62.     }
  63.  
  64.     private CountDownTimer startAutoscroll(ListView listView) {
  65.  
  66.         final long totalScrollTime = 5000;
  67.         final int scrollPeriod = 20;
  68.         final int heightToScroll = 5;
  69.  
  70.         final CountDownTimer timer = new CountDownTimer(totalScrollTime,
  71.                 scrollPeriod) {
  72.             private int height;
  73.  
  74.             public void onTick(long millisUntilFinished) {
  75.                 height = (int) ((totalScrollTime - millisUntilFinished)
  76.                         / scrollPeriod * heightToScroll)
  77.                         + alreadyScrolledHeight;
  78.                 getListView().setSelectionFromTop(
  79.                         getListView().getFirstVisiblePosition(), -height);
  80.             }
  81.  
  82.             public void onFinish() {
  83.                 // saving scroled height, and starting this timer again
  84.                 alreadyScrolledHeight = height;
  85.                 this.start();
  86.             }
  87.         };
  88.  
  89.         getListView().post(new Runnable() {
  90.             @Override
  91.             public void run() {
  92.                 timer.start();
  93.             }
  94.         });
  95.         return timer;
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement