2013-08-24 61 views
0

我正在使用ListView,其中數據是通過使用適配器從資產文件呈現的。 當列表自動滾動時,它不會顯示ListView的底部子代。但是當它觸摸屏幕時,看不見的孩子會變得可見。我想用自動滾動模式顯示它。 下面是代碼ListView autoScroll問題與定時器

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    list = (ListView) findViewById(R.id.list); 
    loadUrduFileInList(); 
    adapter = new LessonListAdapter(this, R.layout.lesson_list_item, urduDataList); 
    list.setAdapter(adapter); 
    list.setOnScrollListener(this); 
    verticalScrollMax = getListViewHeight(list); 
    verticalScrollMax = verticalScrollMax - 921; 
    ViewTreeObserver vto = list.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() 
    { 
     @Override 
     public void onGlobalLayout() 
     { 
      list.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
      verticalScrollMax = getListViewHeight(list); 
      verticalScrollMax = verticalScrollMax - 921; 
      startAutoScrolling(); 
     } 
    }); 
} 

public void startAutoScrolling() 
{ 
    if (scrollTimer == null) 
    { 
     scrollTimer = new Timer(); 
     final Runnable Timer_Tick = new Runnable() 
     { 
      public void run() 
      { 
       moveScrollView(); 
      } 
     }; 

     if (scrollerSchedule != null) 
     { 
      scrollerSchedule.cancel(); 
      scrollerSchedule = null; 
     } 
     scrollerSchedule = new TimerTask() 
     { 
      @Override 
      public void run() 
      { 
       runOnUiThread(Timer_Tick); 
      } 
     }; 

     scrollTimer.schedule(scrollerSchedule, 20, 20); 
    } 
} 
public void moveScrollView() 
{ 
    scrollPos = (int) (list.getScrollY() + 1.0); 
    if (scrollPos >= verticalScrollMax) 
    { 
     scrollPos = 0; 
    } 
    list.scrollTo(0, scrollPos); 
} 

回答

0

我建議,你thath適配器有效的方式來實現。所以這段代碼只是滾動列表視圖

你需要嘗試的變量

final long totalScrollTime = Long.MAX_VALUE; //total scroll time. I think that 300 000 000 years is close enouth to infinity. if not enought you can restart timer in onFinish() 

final int scrollPeriod = 20; // every 20 ms scoll will happened. smaller values for smoother 

final int heightToScroll = 20; // will be scrolled to 20 px every time. smaller values for smoother scrolling 

listView.post(new Runnable() { 
         @Override 
         public void run() { 
           new CountDownTimer(totalScrollTime, scrollPeriod) { 
            public void onTick(long millisUntilFinished) { 
             listView.scrollBy(0, heightToScroll); 
            } 

           public void onFinish() { 
            //you can add code for restarting timer here 
           } 
          }.start(); 
         } 
        }); 
+0

自動滾屏必須是光滑的,而是使用該代碼的另一個值卡會有混蛋。 –