calendarview stops scrolling when nested in scrollview in android

前端 未结 2 1991
盖世英雄少女心
盖世英雄少女心 2021-01-14 00:06

I have nested calendarview in ScrollView . My problem begins when height of view increases. I can scroll calendar months vertically but when scrollview

相关标签:
2条回答
  • 2021-01-14 00:27

    I've found answer to this problem here: https://stackoverflow.com/a/9687545

    Basically you need to implement custom CalendarView and override onInterceptTouchEvent method. I did it as following:

    package com.example.app.views;
    
    public class CalendarViewScrollable extends CalendarView {
    
        public CalendarViewScrollable(Context context) {
            super(context);
        }
    
        public CalendarViewScrollable(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CalendarViewScrollable(Context context, AttributeSet attrs,
                int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev)
        {
            if (ev.getActionMasked() == MotionEvent.ACTION_DOWN)
            {
                ViewParent p = getParent();
                if (p != null)
                    p.requestDisallowInterceptTouchEvent(true);
            }
    
            return false;
        }
    
    }
    

    Then just use this view in XML layout file:

    <com.example.app.views.CalendarViewScrollable
        android:id="@+id/datePicker1"
        android:layout_width="400dp"
        android:layout_height="400dp"
     />
    
    0 讨论(0)
  • 2021-01-14 00:45

    Here's one solution, you can implement a custom ScrollView which will only intercept verticale swipe.

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {...}
    

    "onInterceptTouchEvent" will return false for horizontal swipe, so horizontal swipe will be enable for the CalendarView.

    You can read more about this heree : https://stackoverflow.com/a/2655740/5158173

    0 讨论(0)
提交回复
热议问题