当我们到达Horizo​​ntalScrollView中的最后一项时消失的指标

时间:2014-04-30 16:26:55

标签: java android android-layout mobile horizontalscrollview

我有一个Horizo​​ntalScrollView的子类,我在其中添加了多个图像。 我必须在屏幕的左右两端显示某种指示器,显示左侧和右侧是否有剩余物品。当我们达到相关目的时,相应的指标也应该消失。

有人可以告诉我可用的选项吗?

     protected void onScrollChanged(int l, int t, int oldl, int oldt) {

             View view = (View) takeparent().getChildAt(takeparent().getChildCount()-1);  //this is the last view
             int diff = (view.getRight()-(getRight()+getScrollX()));// Calculate the scrolldiff
             if( diff == 0 ){  // if diff is zero, then the rightmost end has been reached
                 right.setVisibility(View.GONE); //setting the right button invisible
             }


             View viewleft = (View) takeparent().getChildAt(0);  taking the first View 
             int diffleft2 = (viewleft.getLeft()-(getLeft()+getScrollX()));// Calculate the scrolldiff

             if( diffleft2 == 0 ){  // if diff is zero, then the bottom has been reached
                 left.setVisibility(View.GONE);
                 }



             super.onScrollChanged(l, t, oldl, oldt);
     }

1 个答案:

答案 0 :(得分:1)

这是您现在修改过的问题的答案:

@Override
public boolean canScrollHorizontally(int direction)
{
    final int offset = computeHorizontalScrollOffset();
    final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
    if(range == 0){
        return false;
    }
    return (direction < 0) ? (offset > 0) : (offset < range - 1);
}

protected void onScrollChanged(int l, int t, int oldl, int oldt)
{
    left .setVisibility(canScrollHorizontally(-1) ? View.VISIBLE : View.GONE);
    right.setVisibility(canScrollHorizontally( 1) ? View.VISIBLE : View.GONE);
}

我建议Drawables使用Views'。

所以,我已经延长了HorizontalScrollView并覆盖了draw()方法,以便为指标绘制自己的Drawablecanvas。其余部分只是使用setBounds来管理它们的位置,现在是一个按下状态。

代码:https://gist.github.com/slightfoot/4c017f776663bf6dd5d8#file-indicatedhorizontalscrollview-java