W. Hoffman
W. Hoffman

Reputation: 27

Xamarin - Java Library Listeners and Events?

I'm trying to access user-defined listeners in this Android library with Xamarin bindings (original here) for when the calendar is scrolled to another month or a date is selected on the calendar.

The listeners in the code given in the sample are as follows:

        compactCalendarView.setListener(new CompactCalendarView.CompactCalendarViewListener() {
        @Override
        public void onDayClick(Date dateClicked) {
            List<Event> events = compactCalendarView.getEvents(dateClicked);
            Log.d(TAG, "Day was clicked: " + dateClicked + " with events " + events);
        }

        @Override
        public void onMonthScroll(Date firstDayOfNewMonth) {
            Log.d(TAG, "Month was scrolled to: " + firstDayOfNewMonth);
        }
    });

Specifically what I'd like to access is the onDayClick listener. The code for it in its Controller class in Java doesn't specify a button but rather calculates the position in the calendar of the date you clicked and then returns a date based on that calculation.

    void onSingleTapUp(MotionEvent e) {

    // Don't handle single tap when calendar is scrolling and is not stationary

    if (isScrolling()) {

        return;

    }



    int dayColumn = Math.round((paddingLeft + e.getX() - paddingWidth - paddingRight) / widthPerDay);

    int dayRow = Math.round((e.getY() - paddingHeight) / heightPerDay);



    setCalenderToFirstDayOfMonth(calendarWithFirstDayOfMonth, currentDate, monthsScrolledSoFar(), 0);



    int firstDayOfMonth = getDayOfWeek(calendarWithFirstDayOfMonth);



    int dayOfMonth = ((dayRow - 1) * 7) - firstDayOfMonth;

    if (isRtl) {

        dayOfMonth +=  6 - dayColumn;

    } else {

        dayOfMonth += dayColumn;

    }

    if (dayOfMonth < calendarWithFirstDayOfMonth.getActualMaximum(Calendar.DAY_OF_MONTH)

            && dayOfMonth >= 0) {

        calendarWithFirstDayOfMonth.add(Calendar.DATE, dayOfMonth);



        currentCalender.setTimeInMillis(calendarWithFirstDayOfMonth.getTimeInMillis());

        performOnDayClickCallback(currentCalender.getTime());

    }

}

Attempting to declare a listener as CompactCalendarView.ICompactCalendarViewListener listener; seems to declare it fine, but attempting to assign it a new CompactCalendarViewListener() gives me an "Undefined function" error.

I understand C# uses events instead of listeners, but I don't know how it handles user-defined listeners in Java libraries, or how to override events such as onDayClick/onMonthScroll.

Any help is greatly appreciated!

Upvotes: 1

Views: 980

Answers (1)

FreakyAli
FreakyAli

Reputation: 16479

Your classes in Xamarin.Android using C# cannot be anonymous as C# does not support anonymous declarations what you need to do is something like this:

compactCalendarView.SetListener(new CompactCalendarViewListener());

And then define a class that inherits from Java.Lang.Object for Android's Dispose method and the interface that you want to use something like below:

 public class CompactCalendarViewListener : Java.Lang.Object, CompactCalendarView.ICompactCalendarViewListener
{
    public void OnDayClick(Date p0)
    {
        // throw new System.NotImplementedException();
    }

    public void OnMonthScroll(Date p0)
    {
        //throw new System.NotImplementedException();
    }
}

Upvotes: 2

Related Questions