package com.github.badoualy.datepicker;

import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.Locale;

public class MonthView extends RecyclerView {

    private static String[] MONTHS = DateFormatSymbols.getInstance().getShortMonths();

    private MonthAdapter adapter;
    private LinearLayoutManager layoutManager;
    private OnMonthSelectedListener onMonthSelectedListener;

    private int defaultColor, colorSelected, colorBeforeSelection;

    private int startYear = 1970, startMonth = 0;

    private int selectedYear, selectedMonth;
    private int selectedPosition = -1;

    public MonthView(Context context) {
        super(context);
        init();
    }

    public MonthView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MonthView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        final Calendar calendar = Calendar.getInstance();
        setSelectedMonth(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), false);

        setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
        adapter = new MonthAdapter();
        setLayoutManager(layoutManager);
        setAdapter(adapter);
    }

    public void setSelectedMonth(int year, int month) {
        setSelectedMonth(year, month, true, true);
    }

    public void setSelectedMonth(int year, int month, boolean callListener) {
        setSelectedMonth(year, month, callListener, true);
    }

    public void setSelectedMonth(int year, int month, boolean callListener, boolean centerOnPosition) {
        onMonthSelected(year, month, callListener, centerOnPosition);
    }

    public int getSelectedYear() {
        return selectedYear;
    }

    public int getSelectedMonth() {
        return selectedMonth;
    }

    private void onMonthSelected(int year, int month, boolean callListener, boolean centerOnPosition) {
        int oldPosition = selectedPosition;
        selectedPosition = getPositionForDate(year, month);
        selectedYear = year;
        selectedMonth = month;

        if (selectedPosition == oldPosition) {
            if (centerOnPosition)
                centerOnPosition(selectedPosition);
            return;
        }

        if (adapter != null && layoutManager != null) {
            final int rangeStart = Math.min(oldPosition, selectedPosition);
            final int rangeEnd = Math.max(oldPosition, selectedPosition);
            adapter.notifyItemRangeChanged(rangeStart, rangeEnd - rangeStart + 1);

            // Animate scroll
            if (centerOnPosition)
                centerOnPosition(selectedPosition);

            if (callListener && onMonthSelectedListener != null)
                onMonthSelectedListener.onMonthSelected(year, month, selectedPosition);
        } else if (centerOnPosition) {
            post(new Runnable() {
                @Override
                public void run() {
                    centerOnPosition(selectedPosition);
                }
            });
        }
    }

    public void centerOnPosition(int position) {
        if (getChildCount() == 0) {
            return;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (!isLaidOut())
                return;
        }
        // Animate scroll
        int offset = getMeasuredWidth() / 2 - getItemWidth() / 2;
        layoutManager.scrollToPositionWithOffset(position, offset);
    }

    public void centerOnDate(int year, int month) {
        centerOnPosition(getPositionForDate(year, month));
    }

    public void centerOnSelection() {
        centerOnPosition(selectedPosition);
    }

    void scrollToYearPosition(int year, int offsetYear) {
        if (getChildCount() == 0) {
            return;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (!isLaidOut())
                return;
        }
        // Animate scroll
        layoutManager.scrollToPositionWithOffset(getPositionForDate(year + 1, 0), offsetYear + getMeasuredWidth() / 2 - getItemWidth() / 2);
    }

    int getItemWidth() {
        return getChildAt(0).getMeasuredWidth();
    }

    int getYearWidth() {
        return getItemWidth() * 12;
    }

    public int getSelectedPosition() {
        return selectedPosition;
    }

    private int getYearForPosition(int position) {
        return (position + startMonth) / 12 + startYear;
    }

    private int getMonthForPosition(int position) {
        return (startMonth + position) % 12;
    }

    private int getPositionForDate(int year, int month) {
        return (12 * (year - startYear) + month) - startMonth;
    }

    public void setOnMonthSelectedListener(OnMonthSelectedListener onMonthSelectedListener) {
        this.onMonthSelectedListener = onMonthSelectedListener;
    }

    public OnMonthSelectedListener getOnMonthSelectedListener() {
        return onMonthSelectedListener;
    }

    /** Default indicator and text color */
    public void setDefaultColor(int defaultColor) {
        this.defaultColor = defaultColor;
    }

    /** Color when month is selected */
    public void setColorSelected(int colorSelected) {
        this.colorSelected = colorSelected;
    }

    /** Color when month is before the current selected month */
    public void setColorBeforeSelection(int colorBeforeSelection) {
        this.colorBeforeSelection = colorBeforeSelection;
    }

    public int getDefaultColor() {
        return defaultColor;
    }

    public int getColorBeforeSelection() {
        return colorBeforeSelection;
    }

    public int getColorSelected() {
        return colorSelected;
    }

    public void setFirstDate(int startYear, int startMonth) {
        this.startYear = startYear;
        this.startMonth = startMonth;
        selectedYear = startYear;
        selectedMonth = startMonth;
        selectedPosition = 0;
        if (adapter != null)
            adapter.notifyDataSetChanged();
    }

    private class MonthAdapter extends RecyclerView.Adapter<MonthViewHolder> {

        MonthAdapter() {

        }

        @Override
        public MonthViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            final View view = LayoutInflater.from(parent.getContext())
                                            .inflate(R.layout.mti_item_month, parent, false);
            return new MonthViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MonthViewHolder holder, int position) {
            final int year = getYearForPosition(position);
            final int month = getMonthForPosition(position);
            holder.bind(year, month, position == selectedPosition, position < selectedPosition);
        }

        @Override
        public int getItemCount() {
            return Integer.MAX_VALUE;
        }

    }

    private class MonthViewHolder extends RecyclerView.ViewHolder {

        private final TextView lbl;
        private final DotView indicator;

        private int year, month;

        MonthViewHolder(View root) {
            super(root);

            indicator = (DotView) root.findViewById(R.id.mti_view_indicator);
            lbl = (TextView) root.findViewById(R.id.mti_month_lbl);

            root.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    onMonthSelected(year, month, true, true);
                }
            });
        }

        void bind(int year, int month, boolean selected, boolean beforeSelection) {
            this.year = year;
            this.month = month;

            lbl.setText(MONTHS[month].substring(0, 3).toUpperCase(Locale.US));
            int color = selected ? colorSelected : beforeSelection ? colorBeforeSelection : defaultColor;
            lbl.setTextColor(color);
            indicator.setColor(color);
            indicator.setCircleSizeDp(selected ? 12 : 5);
        }
    }

    public interface OnMonthSelectedListener {
        void onMonthSelected(int year, int month, int index);
    }

    public interface DateLabelAdapter {
        CharSequence getLabel(Calendar calendar, int index);
    }
}

@@ -8230,16 +8230,26 @@
 cale.US)
++%22%5Cn%22+year
 );%0A     

Update MonthView.java