替代onSelectedDayChange calendarview android

时间:2013-02-28 13:49:35

标签: android calendarview

onSelectedDayChange的问题在于,当我打开日历视图并点击焦点日时,没有任何反应。例如,我想查看今天的详细信息。现在我打开日历视图,今天是重点。所以我必须点击其他日期点击今天点击查看详细信息。有什么技巧可以解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

CalendarView派生自View,它可以生成onClick()事件。您可以尝试从视图中捕获onClick()事件。只是一个想法

答案 1 :(得分:0)

我做了以下课程来解决这个问题。

public class CustomCalendarView extends CalendarView {

    private final String LOG_HEADER = "CAL:VW";
    private Date previousSelectedDate = new Date();
    private OnDateChangeListener listener;
    private CheckSameSelectedDateAsyncTask task = new CheckSameSelectedDateAsyncTask();

    public CustomCalendarView(Context context) {
        super(context);
    }

    public CustomCalendarView(Context context, AttributeSet attribute) {
        super(context, attribute);
    }

    public CustomCalendarView(Context context, AttributeSet attribute, int defStyle) {
        super(context, attribute, defStyle);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if ((task.getStatus() == AsyncTask.Status.PENDING) || (task.getStatus() == AsyncTask.Status.FINISHED)) {
            task = new CheckSameSelectedDateAsyncTask();
            task.execute();
        }
        return false;
    }

    private void checkSameSelectedDate() {
        Date selectedDate = new Date(super.getDate());
        if (selectedDate.getDay() == previousSelectedDate.getDay() &&
                selectedDate.getMonth() == previousSelectedDate.getMonth() &&
                selectedDate.getYear() == previousSelectedDate.getYear()) {
            if (listener != null) {
                this.listener.onSameSelectedDayChange(this, selectedDate.getYear(), selectedDate.getMonth(), selectedDate.getDay());
            }
        }
        this.previousSelectedDate = selectedDate;
    }

    public void setSameSelectedDayChangeListener(OnDateChangeListener listener) {
        this.listener = listener;
    }

    public interface OnDateChangeListener extends CalendarView.OnDateChangeListener {
        void onSameSelectedDayChange(CalendarView view, int year, int month, int day);
    }

    private class CheckSameSelectedDateAsyncTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                //Note: Breaking point between 75 - 100
                Thread.sleep(300);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            checkSameSelectedDate();
        }
    }
}