Since DataGrid doesn't expose it's ScrollPanel, create an inner class that extends DataGrid and provides a reference to the ScrollPanel:
private static class MyDataGrid<T> extends DataGrid {
public ScrollPanel getScrollPanel() {
HeaderPanel header = (HeaderPanel) getWidget();
return (ScrollPanel) header.getContentWidget();
}
}
Initialize the variables needed for this to work:
private MyDataGrid<MyDataType> myDataGrid;
private int incrementSize = 20;
private int lastScrollPos = 0;
In the constructor, create the grid:
myDataGrid = new MyDataGrid<MyDataType>();
Then add a ScrollHandler, using the getScrollPanel() reference that was just created:
myDataGrid.getScrollPanel().addScrollHandler(new ScrollHandler(){
@Override
public void onScroll(ScrollEvent event) {
int oldScrollPos = lastScrollPos;
lastScrollPos = myDataGrid.getScrollPanel().getVerticalScrollPosition();
// If scrolling up, ignore the event.
if (oldScrollPos >= lastScrollPos) {
return;
}
//Height of grid contents (including outside the viewable area) - height of the scroll panel
int maxScrollTop = myDataGrid.getScrollPanel().getWidget().getOffsetHeight() -
myDataGrid.getScrollPanel().getOffsetHeight();
if(lastScrollPos >= maxScrollTop) {
myDataGrid.setVisibleRange(0,myDataGrid.getVisibleRange().getLength()+incrementSize);
}
}
});