Hide and show the Toolbar according to the scrolling

社会主义新天地 提交于 2020-07-05 11:25:15

问题


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();
                    }

                }
            }
        });
    }
}


来源:https://stackoverflow.com/questions/55857749/hide-and-show-the-toolbar-according-to-the-scrolling

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!