CalendarView today date item click

前端 未结 8 1170
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-18 10:49

I searched a lot on the internet and I couldn\'t succeed to find correct solution for CalendarView click on today date.

8条回答
  •  孤街浪徒
    2021-01-18 11:17

    I made the used the following class. It is a bit of a hack with the thread sleep, but one can increase the sleep time.

    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 {
            @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();
            }
        }
    }
    

提交回复
热议问题