tags:

views:

1581

answers:

2

By infinite scrolling, I mean I initially load a few child widgets, and as the user scrolls down lazily load more to fill it out.

Any ideas appreciated.

(ideally would like something for GWT 1.6 and 1.5)

+4  A: 

Hi, try something like this:

public static class InfiniteScrollPanel implements ScrollHandler {
 String text="Lorem ipsum dolor sit amet, consectetuer...";
 ScrollPanel panel=new ScrollPanel(new HTML(text));
 int height=200;
 int width=200;

 public InfiniteScrollPanel() {
  panel.setHeight(height);
  panel.setWidth(width);
  panel.addScrollHandler(this);   
 }
 public void onScroll(ScrollEvent event) {
  if(panel.getScrollPosition==height) {
    panel.add(new HTML(text));
  }
 }
}

What this code does: it creates a ScrollPanel and adds a ScrollHandler to it. In the ScrollHandler the scrollheight gets compared to the height of the panel and it then adds another child to the panel.

I haven't tested it because I'm wtriting this on a netbook and don't have an IDE on it.

Hope that helps Chris

Chris Boesing
Thanks Chris - I guess the trick is then to now the heights and when to trigger things to being added to the panel etc..
Michael Neale
If you don't know the height you could call getOffsetHeight, but then you need to know the decorations(borders, margin, padding)
Chris Boesing
+2  A: 

I was able to confirm the following works in GWT 1.5 at least:

        final VerticalPanel vp = new VerticalPanel();
    for (int i = 0; i < 40; i++) {
        vp.add(new HTML("Oh oh"));
    }
    final HTML f = new HTML("END");
    vp.add(f);

    final ScrollPanel panel = new ScrollPanel(vp);
    panel.setHeight("20em");
    panel.addScrollListener(new ScrollListener() {

        HTML end = f;
        public void onScroll(Widget widget, int scrollLeft, int scrollTop) {


            int finalPos = end.getAbsoluteTop() + end.getOffsetHeight(); 
            int panelPos = panel.getAbsoluteTop() + panel.getOffsetHeight();
            if (finalPos == panelPos) {
                end = new HTML("MORE !!");
                vp.add(end);
            }


        }
    });

Note the interesting bits are the calculations of the positions.

Michael Neale