HorizontalScrollViews scrolling together

2019-08-14 04:52发布

问题:

I need to scroll 2 parallel HorizontalScrollView dependently, which means that if I'm scrolling one, the other one should scroll too.

How can I do such a thing?

Thanks in advance.

回答1:

hscrollViewOne=(HorizontalScrollView)findViewById(R.id.hsvOne);
hscrollViewTwo=(HorizontalScrollView)findViewById(R.id.hsvTwo);


hscrollViewOne.setOnScrollChangeListener(new View.OnScrollChangeListener() {
        @Override
        public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
            hscrollViewTwo.scrollTo(scrollX, scrollY);
        }
    });

hscrollViewTwo.setOnScrollChangeListener(new View.OnScrollChangeListener() {
        @Override
        public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
            hscrollViewOne.scrollTo(scrollX,scrollY);
        }
    });