2

Is it possible to determine wether or not a specific widget is showing in the viewport?

I want to start loading more data once the last widget in my listwidget is displayed to the user. I figured I could handle scroll events and for every event check if the last widget is displayed and the fire of an rpc to get more data.

BR

per_jansson
  • 2,103
  • 4
  • 29
  • 46

2 Answers2

1

Here's the GWT method that does the Job (it is translated from the JQuery solution here).

/**
 * @param widget the widget to check
 * @return true if the widget is in the visible part of the page
 */
private boolean isScrolledIntoView(Widget widget) {
    if (widget != null) {
        int docViewTop = Window.getScrollTop();
        int docViewBottom = docViewTop + Window.getClientHeight();
        int elemTop = widget.getAbsoluteTop();
        int elemBottom = elemTop + widget.getOffsetHeight();
        return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
    }       
    return false;
}
Community
  • 1
  • 1
Massi
  • 87
  • 3
0

Have a look at the ShowMorePagerPanel from the GWT Showcase's CellList sample.

You can use the widget's getElement()'s getOffsetTop() and the scrolling element's getScrollTop() (equivalent to getVerticalScrollPosition() for a ScrollPanel) to compute whether the widget is currently in view (beware of the elements' getOffsetParent() though, you might want to set the scrolling element's CSS position to relative to make it the offset parent of the widget)

Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164