HorizontalScrollView inside ScrollView - disable vertical scroll when horizontal scroll is in progress

梦想的初衷 提交于 2019-12-03 21:56:52

问题


I have a parent which is ScrollView, which holds a HorizontalScrollView as one of it's child views. All works perfectly, but when someone is triggering vertical scroll, the horizontal scroll stops, or is jittering. On top of it, there is some bug in Ice Cream Sandwich which does not handle vertical movement on HorizontalScrollView well, if it is inside a ScrollView, which content is smaller than the screen (in that situation, there is no vertical scrolling). Jelly Bean android handles it properly, but IceCreamSandwich for some reason detects the vertical movement, and stops the horizontal scrolling.

How would I go about making the parent scrollView disable it's vertical scroll, when a horizontal scroll is being triggered? So when someone is scrolling horizontally the HorizontalScrollView, and while doing it, the finger wonders a bit on a Y Axis, I do not want the parent ScrollView to capture that vertical motion.

Any help with that?


回答1:


just try this

HorizontalScrollView hv = (HorizontalScrollView)findViewById(R.id.myHsView);  // your HorizontalScrollView inside scrollview
    hv.setOnTouchListener(new ListView.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                case MotionEvent.ACTION_DOWN:
                    // Disallow ScrollView to intercept touch events.
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    break;

                case MotionEvent.ACTION_UP:
                    // Allow ScrollView to intercept touch events.
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
                }

                // Handle HorizontalScrollView touch events.
                v.onTouchEvent(event);
                return true;
            }
        });


来源:https://stackoverflow.com/questions/22608596/horizontalscrollview-inside-scrollview-disable-vertical-scroll-when-horizontal

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