CalendarView today date item click

前端 未结 8 1166
爱一瞬间的悲伤
爱一瞬间的悲伤 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:05

    A review of CalendarView.java indicates that the highlighted row is overrided by an internal private class which consumes the touch event, so neither OnClickListener() nor OnTouchListener() will work.

    The first (harder) options is to make your own copy of CalendarView.java (call it MyCustomCalendarView.java) and include it into your project. Within MyCustomCalendarView.java, add code to register an OnTouchListener like so:

    private View.OnTouchListener touchListener;
    @Override
    public void setOnTouchListener( View.OnTouchListener otl) {
        touchListener = otl;
    }
    

    Then in the private class WeeksAdapter's OnTouch() method, add this line:

    @Override
     public boolean onTouch(View v, MotionEvent event) {
         if (mListView.isEnabled() && mGestureDetector.onTouchEvent(event)) {
             touchListener.onTouch(v, event);
         ...
    

    Then MyCustomCalendarView could be used as follows:

    MyCustomCalendarView thisCalendar = (MyCustomCalendarView) rootView.findViewById(R.id.mycustom_calendar_view_layout_id);
    thisCalendar.setOnTouchListener(myCustomListener);
    

    Another (easier) alternative is to add a button to the calendarView layout, and use the button's onClickListener (plus maybe a flag variable) to detect whether any date has been selected. In this case, selecting the current date will be the same as "no date selected" from the device's point of view, so you can use that fact to write in today's date, or other custom actions.

    You can use datetimepicker-library's date picker layout to help you get started:

    
    
        
            
        
    
        
        
    
        
        
            
    
    

提交回复
热议问题