【问题标题】:Indicator which disappear when we have reached last item in a HorizontalScrollView当我们到达 Horizo​​ntalScrollView 中的最后一项时指示器消失
【发布时间】:2014-04-30 16:26:54
【问题描述】:

我有一个 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);
     }

【问题讨论】:

    标签: java android android-layout mobile horizontalscrollview


    【解决方案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() 方法,以将自己的Drawable 绘制到指标的canvas。剩下的只是使用setBounds 管理它们的位置,现在是按下状态。

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

    【讨论】:

    • 你能解释一下你的答案吗?我没听懂你。
    • 抱歉,我还是没听懂你的意思,因为它没有说明如何在达到各自的目标时显示和消失指示器。指标让我们假设是一个按钮
    • 如果您希望指标可点击。然后有一个更好的方法。它是哪一个?你想要可点击的指标吗?与否。
    • 请看看您能否在这个问题上帮助我。我试图解决这个问题 2 天。请参见。 stackoverflow.com/questions/23835529/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多