Search in sources :

Example 1 with ScrollListener

use of com.codename1.ui.events.ScrollListener in project CodenameOne by codenameone.

the class Toolbar method bindScrollListener.

private void bindScrollListener(boolean bind) {
    final Form f = getComponentForm();
    if (f != null) {
        final Container actualPane = f.getActualPane();
        final Container contentPane = f.getContentPane();
        if (bind) {
            initVars(actualPane);
            scrollListener = new ScrollListener() {

                public void scrollChanged(int scrollX, int scrollY, int oldscrollX, int oldscrollY) {
                    int diff = scrollY - oldscrollY;
                    int toolbarNewY = getY() - diff;
                    if (scrollY < 0 || Math.abs(toolbarNewY) < 2) {
                        return;
                    }
                    toolbarNewY = Math.max(toolbarNewY, -getHeight());
                    toolbarNewY = Math.min(toolbarNewY, initialY);
                    if (toolbarNewY != getY()) {
                        setY(toolbarNewY);
                        if (!layered) {
                            actualPane.setY(actualPaneInitialY + toolbarNewY);
                            actualPane.setHeight(actualPaneInitialH + getHeight() - toolbarNewY);
                            actualPane.doLayout();
                        }
                        f.repaint();
                    }
                }
            };
            contentPane.addScrollListener(scrollListener);
            releasedListener = new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    if (getY() + getHeight() / 2 > 0) {
                        showToolbar();
                    } else {
                        hideToolbar();
                    }
                    f.repaint();
                }
            };
            contentPane.addPointerReleasedListener(releasedListener);
        } else {
            if (scrollListener != null) {
                contentPane.removeScrollListener(scrollListener);
                contentPane.removePointerReleasedListener(releasedListener);
            }
        }
    }
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) ScrollListener(com.codename1.ui.events.ScrollListener)

Aggregations

ActionEvent (com.codename1.ui.events.ActionEvent)1 ActionListener (com.codename1.ui.events.ActionListener)1 ScrollListener (com.codename1.ui.events.ScrollListener)1