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

后端 未结 1 1085
臣服心动
臣服心动 2021-01-06 13:40

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

相关标签:
1条回答
  • 2021-01-06 13:50

    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;
                }
            });
    
    0 讨论(0)
提交回复
热议问题