Question

I have a subclass of HorizontalScrollView in which I am adding multiple images. I have to show some kind of indicator at left and right end of screen which shows if there are items remaining on left and right side. Also the respective indicator should disappear when we have reached relevant end.

Can someone tell me the options available for this?

     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);
     }
Was it helpful?

Solution

This is the answer to your now modified question:

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

I would recommend Drawables over using Views'.

So, I've extended HorizontalScrollView and overridden the draw() method to draw its own Drawable's to the canvas for the indicators. The rest of it is simply managing the position of them using setBounds, and now a pressed state.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top