Horizontal ScrollView full Scroll focus right with ViewTreeObserver

柔情痞子 提交于 2019-12-11 20:39:17

问题


I am adding the objects into the horizontal scrollview dynamically and when i add the code below works fine to move extreme right, now the problem is when i try to scroll back to left side via swapping finger the code below again runs and horizontal scrollview scrolls to the right corner again, its a bit annoying, anyone has some alternate solution, I ve tried the

Postdelayed()

but it doesnt work properly for me

ViewTreeObserver viewTreeObserver = hScrollView.getViewTreeObserver();
        if (viewTreeObserver.isAlive()) {
            viewTreeObserver
                    .addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {
                            // interestedInView is ready for size and position
                            // queries because it has been laid out
                            hScrollView
                                    .fullScroll(HorizontalScrollView.FOCUS_RIGHT);
                        }
                    });
        }

Regards MGD


回答1:


is your layoutlistener there to scroll your view all the way to the right when your scrollview is created? If so you should then remove the event listener inside your onGlobalLayout function, otherwise it will scroll right whenever it gets laid out.



来源:https://stackoverflow.com/questions/9414035/horizontal-scrollview-full-scroll-focus-right-with-viewtreeobserver

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