calendarview stops scrolling when nested in scrollview in android

回眸只為那壹抹淺笑 提交于 2019-12-01 07:07:57

问题


I have nested calendarview in ScrollView . My problem begins when height of view increases. I can scroll calendar months vertically but when scrollview scrollbar comes it does not let scroll calendar. Instead of scrolling calendar the whole view scroll up or down.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:fillViewport="true">

<LinearLayout android:orientation="vertical" android:id="@+id/ReminderLayout" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" >


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/TaskName"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:inputType="textCapWords"
                android:singleLine="true"
                android:textSize="26.0sp" />

            <View
                android:id="@+id/seperator"
                android:layout_width="fill_parent"
                android:layout_height="1dip" />

            <TextView
                android:id="@+id/lblNotes"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Notes" />

            <EditText
                android:id="@+id/TaskNotes"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:inputType="textMultiLine"
                android:textSize="26.0sp" />

            <View
                android:id="@+id/seperator"
                android:layout_width="fill_parent"
                android:layout_height="1dip" />

            <TextView
                android:id="@+id/lblReminder"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Reminder" />
        </LinearLayout>

        <LinearLayout android:orientation="horizontal" 
            android:layout_width="fill_parent" android:layout_height="wrap_content">"

            <CalendarView
                android:id="@+id/calendarReminder"
                android:layout_width="fill_parent"
                android:layout_height="250dp" 
                android:scrollbars="vertical"
                android:isScrollContainer="true" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
           android:layout_height="wrap_content"
            android:orientation="horizontal"
            >

            <TimePicker
                android:id="@+id/timePickerReminder"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:gravity="center" />

        </LinearLayout>


        <LinearLayout android:orientation="vertical" 
            android:layout_width="fill_parent" android:layout_height="wrap_content" >
            <View android:id="@+id/seperator" android:layout_width="fill_parent" android:layout_height="1dip"/>
            <TextView android:id="@+id/ReminderTime" android:layout_width="fill_parent" android:layout_height="wrap_content"
                 android:text="On Time" android:paddingLeft="20dip"/>
            <View android:id="@+id/seperator" android:layout_width="fill_parent" android:layout_height="1dip"/>
            <TextView android:id="@+id/Recurring" android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:text="Only Once" android:paddingLeft="20dip"/>
        </LinearLayout> 
</LinearLayout>
</ScrollView>

回答1:


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"
 />



回答2:


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



来源:https://stackoverflow.com/questions/18782919/calendarview-stops-scrolling-when-nested-in-scrollview-in-android

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