问题描述
我有两个水平滚动视图,并且希望始终将它们保持在相同的位置/距离.如果用户滚动一个,则需要以编程方式滚动另一个.挑战在于,将发生无限循环.一个会加薪,另一个会加薪.如何设置状态,指示用户启动的滚动仍在进行中?因此,其他滚动视图不应执行程序化滚动.
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中同步两个滚动视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!