How to synchronize two scrollview in Android?

可紊 提交于 2019-12-22 10:36:07

问题


I have two horizontal scrollview, and want to keep them always at the same position / distance. If user scrolls one, need to scroll programmatically the other. The challenge is, that an infinite loop will occur. One will raise the other, other will raise first. How can I set a state, indicate that a user initiated scroll is still in progress? So other scrollview should not execute the programmatic scroll.

One of them is a HorizontalScrollView other is a RecyclerView.

Tried solutions like below, without any success:

    horizontalScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {

        @Override
        public void onScrollChanged() {

            if (programmaticScrollEnable) {
                programmaticScrollEnable = false;
                // do programmatic scrolling here
                programmaticScrollEnable = true;
            }
        }
    });

Tried to change state in onScrollStateChanged method:

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                /*if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    MainActivity.programmaticScrollEnable = true;
                }*/
            }

回答1:


Try this...

horizontalScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {

    @Override
    public void onScrollChanged() {

   recyclerView.scrollTo(horizontalScrollView.scrollTo(horizontalScrollView.getScrollX(), 0);

    }
});


来源:https://stackoverflow.com/questions/38665677/how-to-synchronize-two-scrollview-in-android

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