How to find out which item is displayed in the GWT ScrollPanel

Question about the GWT ScrollPanel.

Is there a way to determine which child is displayed in the ScrollPanel?

(Of course, the ScrollPanel contains a DecoratorPanel that has an HTML object)

+4
source share
3 answers

AFAIK there are no built-in functions for this, not in the DOM and in GWT.

If you have fixed height elements, you can try to calculate whether any element is displayed: Check if the element is visible after scrolling

0
source

Here we use the GWT method, which performs the task (it is translated from the jQuery solution proposed above).

/** * @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; } 
+1
source

If you want to check if the widget is visible on the scroll bar, you can do it this way (similar to Massi's solution):

  /** * @param widget the widget to check * @return true if the widget is in the visible part of the scroll panel */ private boolean isVisibleInScrollPanel(Widget widget, ScrollPanel scrollPanel) { if (widget != null) { int containerTop = scrollPanel.getAbsoluteTop(); int containerBottom = containerTop + scrollPanel.getOffsetHeight(); int widgetTop = widget.getAbsoluteTop(); int widgetBottom = widgetTop + widget.getOffsetHeight(); return ((widgetBottom <= containerBottom) && (widgetTop >= containerTop)); } return false; } 

I used this method as follows:

 // When the selected widget is invisible, then we ensure it to be visible if (!isVisibleInScrollPanel(widget, scrollPanel)) { scrollPanel.ensureVisible(widget); } 
0
source

Source: https://habr.com/ru/post/1369424/


All Articles