带箭头的Horizo​​ntalScrollView

时间:2011-10-06 09:12:40

标签: android horizontalscrollview

我正在尝试使用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>

2 个答案:

答案 0 :(得分:4)

Horizo​​ntalScrollView没有滚动侦听器。你可以做的是向它添加一个OnTouchListener这将在用户滚动时连续触发,每次你可以使用返回int的getScrollX方法。

现在0表示最左边,为了找到最右边(最大滚动量),你需要找到水平滚动视图的子视图的宽度。这可以通过使用addOnGlobalLayoutListener找到,否则宽度在onCreate方法中总是为0

将此代码放入onCreate方法

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;
    }
});

答案 1 :(得分:3)

使用滚动条内的LinearLayout的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

相关问题