Hide and show the Toolbar according to the scrolli

2020-07-23 04:24发布

问题:

This question is referring to Codename One only.

I need to make the Toolbar of a Codename One Form moving as shown in this video: https://www.informatica-libera.net/videoLavoro/hideShowToolbarOnScrolling.mp4

As you can see, the scroll up causes the Toolbar to gradually disappear, while the scroll down causes the Toolbar to gradually reappear.

A solution like https://stackoverflow.com/a/55856590 is not applicable because I don't need to change the UIID of the Toolbar, but I need to move the Toolbar up and down during the scroll, to get the same effect shown in the video.

回答1:

This is the approach we took in the whatsapp clone application for toolbar as that's the exact behavior of whatsapp. There is more to it but this block contains most of the logic that implements this:

private void bindFolding(Container titleArea, int titleHeight, 
        Container... scrollables) {
    addPointerReleasedListener(e -> {
        if(titleArea.getHeight() != titleHeight && 
                    titleArea.getHeight() != 0) {
            if(titleHeight - titleArea.getHeight() > titleHeight / 2) {
                titleArea.setPreferredSize(null);
            } else {
                titleArea.setPreferredH(0);
            }
            titleArea.getParent().animateLayout(100);
        }
    });
    for(Container c : scrollables) {
        c.addScrollListener((scrollX, scrollY, oldscrollX,
            oldscrollY) -> {
            // special case for tensile drag
            if(scrollY <= 10) {
                titleArea.setPreferredSize(null);
                return;
            }
            int diff = oldscrollY - scrollY;
            if(diff > 0) {
                if(titleArea.getHeight() < titleHeight) {
                    titleArea.setPreferredH(Math.min(titleHeight, 
                        titleArea.getPreferredH() + diff));
                    titleArea.setHeight(titleArea.getPreferredH());
                    titleArea.getParent().revalidate();
                }
            } else {
                if(diff < 0) {
                    if(titleArea.getHeight() > 0) {
                        titleArea.setPreferredH(Math.max(0, 
                            titleArea.getPreferredH() + diff));
                        titleArea.setHeight(titleArea.getPreferredH());
                        titleArea.getParent().revalidate();
                    }

                }
            }
        });
    }
}


标签: codenameone