Horizo​​ntalScrollView箭头 [英] HorizontalScrollView with arrows

查看:814
本文介绍了Horizo​​ntalScrollView箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用Horizo​​ntalScrollView使用如下所示的布局可滚动的横向菜单。使用previous /下一个箭头按钮或甩菜单滚动。当Horizo​​ntalScrollView达到一结束,我想在同年底被隐藏的箭头(如下图我的形象在想向左箭头将被隐藏)。

我如何能够检测到Horizo​​ntalScrollView已经到头了?

感谢

 < RelativeLayout的机器人:背景=@可绘制/ bg_home_menu
    机器人:layout_width =FILL_PARENT机器人:layout_height =45dp>
    < ImageView的机器人:ID =@ + ID / previous机器人:SRC =@可绘制/ arrow_l
        机器人:layout_height =14dp机器人:layout_width =10dp
        机器人:layout_alignParentLeft =真
        机器人:layout_centerVertical =真/>

    < ImageView的机器人:ID =@ + ID /下一个机器人:SRC =@可绘制/ arrow_r
        机器人:layout_height =14dp机器人:layout_width =10dp
        机器人:layout_alignParentRight =真
        机器人:layout_centerVertical =真/>

    < Horizo​​ntalScrollView机器人:ID =@ + ID / horizo​​ntalScroll
        机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT
        机器人:滚动条=无机器人:fadingEdgeLength =30dp机器人:layout_toLeftOf =@ ID /下一个
        机器人:layout_toRightOf =@ ID / previous>

        <的LinearLayout机器人:ID =@ + ID / home_menu
            机器人:方向=横向机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =FILL_PARENT安卓重力=center_vertical>

            <按钮机器人:ID =@ + ID / btn_home机器人:背景=@可绘制/ btn_home_menu_on
                机器人:layout_height =35dp机器人:layout_width =WRAP_CONTENT机器人:可聚焦=真
                android_clickable =假的android:文本=@字符串/ menu_home机器人:文字颜色=#FFFFFF机器人:标签= -  1/>

            <! - 更多的动态添加 - >

        < / LinearLayout中>

    < / Horizo​​ntalScrollView>
< / RelativeLayout的>
 

解决方案

使用的 getScrollX()与滚轮内侧,以确定它是在屏幕上的最左边角落。如果是0,那么左箭头应该被禁用。

有关右箭头,检索<一href="http://developer.android.com/reference/android/view/View.html#getDrawingRect%28android.graphics.Rect%29"相对=nofollow>绘制矩形的宽度,增加getScrollX()和比较,为的 getMeasuredWidth(),如果​​是一样的,你是在正确的,没有必要的右箭头。

所以,你的code将是这样的:

 如果(homeMenu.getScrollX()== 0){
  hideLeftArrow();
} 其他 {
  showLeftArrow();
}
如果(homeMenu.getDrawingRect()。右== homeMenu.getMeasuredWidth()){
  hideRightArrow();
} 其他 {
  showRightArrow();
}
 

当然,你必须把这个事件侦听器。我只能想到用自己的Horizo​​ntalScrollView类,与onScroll()方法覆盖进行检查不同,请参阅<一href="http://stackoverflow.com/questions/6461991/listening-to-scroll-events-horizontalscrollview-android">this帖子。

I'm trying to make a scrollable horizontal menu using HorizontalScrollView using the layout shown below. The menu is scrollable using the previous/next arrow buttons or on fling. When the HorizontalScrollView reach one end I'd like the arrow at the same end to be hidden (in the image shown below I'd like the left arrow to be hidden).

How can I detect that the HorizontalScrollView has reached an end?

Thanks

<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>

解决方案

Use getScrollX() of the LinearLayout inside your scroller to determine which is the leftmost corner on screen. If it's 0, then left arrow should be disabled.

For the right arrow, retrieve the drawing rectangle's width, add getScrollX() and compare that to getMeasuredWidth (), if it's the same, you're at the right, no need for the right arrow.

So your code will look like this:

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

Of course you will have to put this in an event listener. I can only think of using your own HorizontalScrollView class, with the onScroll() method overwritten to perform the checks above, see this post.

这篇关于Horizo​​ntalScrollView箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆