2011-10-06 67 views
5

我想使用Horizo​​ntalScrollView製作一個可滾動的水平菜單,使用下面顯示的佈局。菜單可以使用上一個/下一個箭頭按鈕或一次性滾動進行滾動。當Horizo​​ntalScrollView到達一端時,我希望隱藏在同一端的箭頭(在下面顯示的圖像中,我希望左箭頭被隱藏)。帶有箭頭的Horizo​​ntalScrollView

如何檢測到Horizo​​ntalScrollView已到達結尾?

感謝

enter image description here

<RelativeLayout android:background="@drawable/bg_home_menu" 
    android:layout_width="fill_parent" android:layout_height="45dp"> 
    <ImageView android:id="@+id/previous" android:src="@drawable/arrow_l" 
     android:layout_height="14dp" android:layout_width="10dp" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" /> 

    <ImageView android:id="@+id/next" android:src="@drawable/arrow_r" 
     android:layout_height="14dp" android:layout_width="10dp" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" /> 

    <HorizontalScrollView android:id="@+id/horizontalScroll" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:scrollbars="none" android:fadingEdgeLength="30dp" android:layout_toLeftOf="@id/next" 
     android:layout_toRightOf="@id/previous" > 

     <LinearLayout android:id="@+id/home_menu" 
      android:orientation="horizontal" android:layout_width="wrap_content" 
      android:layout_height="fill_parent" android:gravity="center_vertical"> 

      <Button android:id="@+id/btn_home" android:background="@drawable/btn_home_menu_on" 
       android:layout_height="35dp" android:layout_width="wrap_content" android:focusable="true" 
       android_clickable="false" android:text="@string/menu_home" android:textColor="#FFFFFF" android:tag="-1"/> 

      <!-- More are added dynamically --> 

     </LinearLayout> 

    </HorizontalScrollView> 
</RelativeLayout> 

回答

3

使用getScrollX()你的滾輪內,以確定哪些是最左邊的內屏幕上的角落。如果它是0,則應該禁用左箭頭。

對於右箭頭,檢索drawing rectangle的寬度,添加getScrollX()並將其與getMeasuredWidth()進行比較,如果相同,則表示您在右邊,不需要右箭頭。

所以,你的代碼看起來就像這樣:

if (homeMenu.getScrollX()==0) { 
    hideLeftArrow(); 
} else { 
    showLeftArrow(); 
} 
if (homeMenu.getDrawingRect().right == homeMenu.getMeasuredWidth()) { 
    hideRightArrow(); 
} else { 
    showRightArrow(); 
} 

當然,你必須把這個事件偵聽器。我只能想到使用自己的Horizo​​ntalScrollView類,並覆蓋onScroll()方法來執行上面的檢查,請參閱this post

4

沒有爲Horizo​​ntalScrollView沒有滾動監聽器。你可以做的是向它添加一個OnTouchListener這將會在用戶滾動時連續觸發,並且每次你可以使用getScrollX方法返回int。

現在0意味着它的最左邊,要找到最右邊(最大滾動量),您需要找到水平滾動視圖的子視圖的寬度。這是通過使用addOnGlobalLayoutListener否則寬度發現將始終爲0的onCreate方法

將這個代碼在onCreate方法中的LinearLayout的

final HorizontalScrollView hs = (HorizontalScrollView)findViewById(R.id.scroll); 

ViewTreeObserver vto = hs.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     hs.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
     maxScrollX = hs.getChildAt(0) 
       .getMeasuredWidth()-getWindowManager().getDefaultDisplay().getWidth(); 

    } 
});   

hs.setOnTouchListener(new OnTouchListener() {   
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     Log.e("ScrollValue", Integer.toString(hs.getScrollX())); 
    if(hs.getScrollX() == maxScrollX){ 
     Log.e("MaxRight", "MaxRight");  
    } 
    return false; 
    } 
}); 
+0

對不起,我不明白它如何解決我的問題... – jul

+1

我已經更新了我的答案。如果這不起作用這可能是幫助http://stackoverflow.com/questions/3948934/synchronise-scrollview-scroll-positions-android – blessenm

+1

+1感謝很多,像魅力工作。真的非常有幫助。 –

相關問題