1

Ich muss eine HorizontalScrollView automatisch scrollen lassen.Android HorizontalScrollView Wiederholtes automatisches Scrollen in einer Schleife

Durch die automatische, ich meine, es sollte:

  1. Glatte Scrollen von der linken automatisch
  2. nach rechts Sobald der rechte Rand erreicht, glatte Scroll rückwärts heißt von rechts nach links.
  3. Wiederholen 1 & 2 unendlich.

Ich habe die folgende Implementierung versucht, die nicht vollständig funktioniert. (Die Ansicht blättert von L bis R, aber nicht von R nach L scrollen)

Timer timer = new Timer("horizontalScrollViewTimer"); 
timer.scheduleAtFixedRate(new TimerTask() { 
    public void run() { 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 

       if (horizontalScrollView.canScrollHorizontally(View.FOCUS_RIGHT)) { 
        horizontalScrollView.smoothScrollBy(5, 0); 
       } else { 
        while (horizontalScrollView.canScrollHorizontally(View.FOCUS_LEFT)) { 
         horizontalScrollView.smoothScrollBy(-5, 0); 
         if (!horizontalScrollView.canScrollHorizontally(View.FOCUS_LEFT)) { 
          break; 
         } 
        } 
       } 

      } 
     }); 
    } 
}, 3000, 50); 

Jede mögliche Hilfe geschätzt wird. TIA.

Antwort

0

ändern und diese versuchen ....

private boolean scrollingLeft = false; 

.... 

Timer timer = new Timer("horizontalScrollViewTimer"); 
    timer.scheduleAtFixedRate(new TimerTask() { 
     @Override 
     public void run() { 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        if (scrollingLeft) { 
         if (horizontalScrollView.getScrollX() == 0) { 
          horizontalScrollView.smoothScrollBy(5, 0); 
          scrollingLeft = false; 
         } else { 
          horizontalScrollView.smoothScrollBy(-5, 0); 
         } 
        } else { 
         if (horizontalScrollView.canScrollHorizontally(View.FOCUS_RIGHT)) { 
          horizontalScrollView.smoothScrollBy(5, 0); 
         } else { 
          horizontalScrollView.smoothScrollBy(-5, 0); 
          scrollingLeft = true; 
         } 
        } 
       } 
      }); 
     } 
    }, 3000, 50);