Android scrollview inside another scrollview doesn't scroll

后端 未结 9 565
北恋
北恋 2020-12-10 02:36

I\'m trying to put a ScrollView inside another ScrollView and I tried the answers found on this site but it still doesn\'t seems to work completely. Here is the XML:

9条回答
  •  一向
    一向 (楼主)
    2020-12-10 03:30

    I use RectF to test whether you finger locates in the range of your child_view.

    I've used it for a case that a mix between scrollview and viewpager(that contains two pages with recycleviews).

    Try these codes and you will get what you wanna.

    findViewById(R.id.parent_view).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                View childV = findViewById(R.id.child_view);
                if (childV != null) {
                    int[] l = new int[2];
                    childV.getLocationOnScreen(l);
                    RectF rect = new RectF(l[0], l[1], l[0] + childV.getWidth(), l[1] + childV.getHeight());
                    if(rect.contains(  event.getX(), event.getY())) {
                        childV.getParent()
                                .requestDisallowInterceptTouchEvent(false);
                        childV.onTouchEvent(event);
                        return true;
                    }
                }
                childV.getParent()
                        .requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });
    

提交回复
热议问题