android中的两个嵌套垂直滚动视图

时间:2015-04-10 04:36:18

标签: android android-scrollview

我正在使用两个垂直滚动视图(例如,父和子)。问题是我无法滚动我的子视图而不是滚动我的整个父视图,是否有任何方法可以限制我的父母滚动我的子滚动视图时滚动视图,反之亦然?

需要帮助......在此先感谢.. !!

4 个答案:

答案 0 :(得分:3)

您需要处理触摸事件才能有效地执行此操作

        outerScrollView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                findViewById(R.id.inner_scroll).getParent()
                        .requestDisallowInterceptTouchEvent(false);
                return false;
            }
        });

        innerScrollView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                v.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });

答案 1 :(得分:1)

试试这个

注意:这里parentScrollView表示外部ScrollView,而childScrollView表示Innner ScrollView

parentScrollView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
        Log.v(TAG, "PARENT TOUCH");

        findViewById(R.id.child_scroll).getParent()
                .requestDisallowInterceptTouchEvent(false);
        return false;
    }
});

childScrollView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
        Log.v(TAG, "CHILD TOUCH");

        // Disallow the touch request for parent scroll on touch of  child view
        v.getParent().requestDisallowInterceptTouchEvent(true);
        return false;
    }
});

答案 2 :(得分:1)

这更适合理解:

childScrollView.setOnTouchListener(new View.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.
                            parentScrollV.requestDisallowInterceptTouchEvent(true);

                            break;

                        case MotionEvent.ACTION_UP:
                            // Allow ScrollView to intercept touch events.
                            parentScrollV.requestDisallowInterceptTouchEvent(false);

                            break;
                    }
                    return false;
                }
            });

答案 3 :(得分:-2)

- 这是您可以找到的SOlution与描述Here.

    m_parentScrollView.setOnTouchListener(new View.OnTouchListener() 
    {
           public boolean onTouch(View p_v, MotionEvent p_event) 
            {
                   m_childScrollView.getParent().requestDisallowInterceptTouchEvent(false);
               //  We will have to follow above for all scrollable contents
               return false;
            }
    });


m_childScrollView.setOnTouchListener(new View.OnTouchListener() 
{
      public boolean onTouch(View p_v, MotionEvent p_event)
       {
          // this will disallow the touch request for parent scroll on touch of child view
           p_v.getParent().requestDisallowInterceptTouchEvent(true);
           return false;
       }
});

//我们必须按照上面的所有子可滚动内容进行操作 我

这两种方法可以解决你的问题。

相关问题