2014-09-04 100 views
1

我得到了一個listView,其中包含全屏中具有相同高度的多個項目。當我釋放滾動時,我想自動滾動到該項目,這會佔用屏幕上最多的空間。當我滾動到一個隨機位置並點擊一個按鈕(例如,當firstVisible項目的60%可見並且以下項目的40%可見時,它會自動順利滾動到第一個可見項目, OnClick)看代碼:onScrollStateChanged在Scroll_State_Idle時不會平滑滾動

ListView mylistView= (ListView)findViewById(R.id.ListViewLayout); 
    int midScreen = mylistView.getMeasuredHeight()/2; 

    public void onClick(View v) { 

    View v = mylistView.getChildAt(0); 
    int top = (v == null) ? 0 : v.getTop(); 
    top =-top; 
    if (top <= midScreen) { 
     mylistView.smoothScrollBy(-top, 1000); // if first item takes more space, scroll up 
     } else { 
      mylistView.smoothScrollBy((midScreen*2)-top, 1000); // if second item takes more space, scroll down 
      } 

現在我想要這個工作過程被調用,當列表停止滾動。我創建了一個onScrollListener和onScrollStateChanged我想這如果listScroll停止(所以狀態0,空閒):

 @Override 
     public void onScrollStateChanged(AbsListView view, int scrollState) { 
      if (scrollState==OnScrollListener.SCROLL_STATE_IDLE) {     
       View vX = mylistView.getChildAt(0); 
       int top = (vX == null) ? 0 : vX.getTop(); 
       top =-top; 
       if (top <= midScreen) { 
        mylistView.smoothScrollBy(-top, 1000); 
       } else mylistView.smoothScrollBy((midScreen*2)-top, 1000); 
      } 
     } 

的問題:不是滾動平滑1秒到準確的位置,以顯示全屏想要的項目,它跳得儘可能快。 smoothScrollBy在狀態0中被調用時不再平滑。爲什麼是這樣的,並且有沒有解決方案?

回答

0

好吧,我設法運行一個可處理的runnable中的smoothScroll,現在它的工作原理! 如果有人有同樣的問題,這裏來的代碼

listViewAnlagen.setOnScrollListener(new OnScrollListener() { 

     @Override 
     public void onScroll(AbsListView view, int firstVisibleItem, 
       int visibleItemCount, int totalItemCount) { 
      } 

     @Override 
     public void onScrollStateChanged(AbsListView view, int scrollState) { 

      if (scrollState==OnScrollListener.SCROLL_STATE_IDLE) { 

       View vX = listViewAnlagen.getChildAt(0); 
       int top = (vX == null) ? 0 : vX.getTop(); 
       top =-top; 
       if (top <= mitteScreen) {      
        handler.post(obenRunnable); 
       } else { 
        handler.post(untenRunnable); 
        } 

      } 
     } 

     Handler handler = new Handler(); 
     Runnable obenRunnable = new Runnable() 
     { 
      public void run() 
      { 

       View vX = listViewAnlagen.getChildAt(0); 
       int top = (vX == null) ? 0 : vX.getTop(); 
       top =-top; 
       listViewAnlagen.smoothScrollBy(-top, 200); 
      } 
     }; 
     Runnable untenRunnable = new Runnable() 
     { 
      public void run() 
      { 
       View vX = listViewAnlagen.getChildAt(0); 
       int top = (vX == null) ? 0 : vX.getTop(); 
       top =-top; 
       listViewAnlagen.smoothScrollBy((mitteScreen*2)-top, 200); 
      } 
     }; 
    }); 
0

我想你可以做一些這樣的事情,讓順利滾動。

public void scrollSmoothTopAfterMiddle(final int top) { 

    final ValueAnimator anim = ValueAnimator.ofInt(top, 0); 

    anim.addUpdateListener(new AnimatorUpdateListener() { 
     @Override 
     public void onAnimationUpdate(final ValueAnimator animation) { 
      final int value = (Integer) animation.getAnimatedValue(); 

      mListView.smoothScrollBy((midScreen * 2) - top, value); 
     } 
    }); 

    anim.setDuration(250); 
    anim.setInterpolator(new DecelerateInterpolator()); 
    anim.start(); 
} 

public void scrollSmoothTopBeforeMiddle(final int top) { 

    final ValueAnimator anim = ValueAnimator.ofInt(top, 0); 

    anim.addUpdateListener(new AnimatorUpdateListener() { 
     @Override 
     public void onAnimationUpdate(final ValueAnimator animation) { 
      final int value = (Integer) animation.getAnimatedValue(); 
      mListView.smoothScrollBy(-top, value); 
     } 
    }); 

    anim.setDuration(250); 
    anim.setInterpolator(new DecelerateInterpolator()); 
    anim.start(); 
} 

if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) { 

    final View vX = mListView.getChildAt(0); 

    int top = (vX == null) ? 0 : vX.getTop(); 

    top = -top; 

    if (top <= midScreen) { 

     scrollSmoothTopBeforeMiddle(-top); 
    } else { 

     scrollSmoothTopAfterMiddle((midScreen * 2) - top); 
    } 
} 
+0

謝謝@anderson_acs的幫助。我對代碼進行了一些優化(只轉發頂部:scrollSmoothTopAfterMiddle(頂部)),但現在它快速滾動而不像以前那樣將動畫移動到正確的位置,並且滾動所需的距離,但是SMOOTHLY。換句話說,我釋放手指在滾動 - >右側位置非平滑滾動 - >進一步滾動到右側位置平滑滾動的距離。請問我是否表達自己的sl。。任何改進想法?再次感謝 – Simsala 2014-09-05 09:22:49

+0

@Simsala,我很抱歉,但我無法真正理解你想說什麼,你試圖增加哪些部分? – 2014-09-05 15:18:21