如何在Android中同步两个滚动视图? [英] How to synchronize two scrollview in Android?

查看:127
本文介绍了如何在Android中同步两个滚动视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个水平滚动视图,并且希望始终将它们保持在相同的位置/距离.如果用户滚动一个,则需要以编程方式滚动另一个.挑战在于,将发生无限循环.一个会加薪,另一个会加薪.如何设置状态,指示用户启动的滚动仍在进行中?因此,其他滚动视图不应执行程序化滚动.

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.

其中一个是HorizontalScrollView,另一个是RecyclerView.

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

试图通过onScrollStateChanged方法更改状态:

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;
                }*/
            }

推荐答案

尝试一下...

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

    @Override
    public void onScrollChanged() {

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

    }
});

这篇关于如何在Android中同步两个滚动视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆