2011-03-23 96 views
3

我想做一個Horizo​​ntalScrollView分頁。如果您將屏幕移動到右側,則會顯示正確的「頁面」,如果將屏幕移動到左側,則會顯示左側的「頁面」。Horizo​​ntalScrollView分頁

+1

嘗試使用具有項目寬度的圖庫作爲頁面寬度。 – Karan 2011-03-23 15:07:33

回答

12

我已經做過這件事了。你可以通過一個自定義的觸摸監聽器來做到這一點:

public MyHorizontalScrollView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    setOnTouchListener(new View.OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 
       if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL){ 
        int scrollX = getScrollX(); 
        int itemWidth = getMeasuredWidth(); 
        int activeItem = ((scrollX + itemWidth/2)/itemWidth); 
        int scrollTo = activeItem * itemWidth; 
        smoothScrollTo(scrollTo, 0); 

        return true; 
       } else { 
        return false; 
       } 
      } 
     }); 
    } 

我覺得很不言自明。這假設你的頁面寬度是恆定的並且等於整個滾動視圖的寬度。

相關問題