HorizontalCalendar

 收藏    我在用   0 人在用

一个基于 RecyclerView 实现的material风格开源日历控件

 GitHub     下载     同步

39    935    169    8 months前

Horizontal Calendar

Download License

A material horizontal calendar view for Android based on RecyclerView.

Installation

The library is hosted on jcenter, add this to your build.gradle:

repositories {
      jcenter()
    }

    dependencies {
      compile 'devs.mulham.horizontalcalendar:horizontalcalendar:1.0.0'
    }

Prerequisites

The minimum API level supported by this library is API 9 (GINGERBREAD).

Usage

Add HorizontalCalendarView to your layout file, for example:

<android.support.design.widget.AppBarLayout
        ............ >

        <devs.mulham.horizontalcalendar.HorizontalCalendarView
            android:id="@+id/calendarView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            app:textColorSelected="#FFFF"/>

</android.support.design.widget.AppBarLayout>

In your Activity, define your start and end dates to set the range of the calendar:

/** end after 1 month from now */
Calendar endDate = Calendar.getInstance();
endDate.add(Calendar.MONTH, 1);

/** start before 1 month from now */
Calendar startDate = Calendar.getInstance();
startDate.add(Calendar.MONTH, -1);

Then setup HorizontalCalendarView using its Builder and pass the id:

HorizontalCalendar horizontalCalendar = new HorizontalCalendar.Builder(this, R.id.calendarView)
                .startDate(startDate.getTime())
                .endDate(endDate.getTime())
                .build();

To listen to date change events you need to set a listener:

horizontalCalendar.setCalendarListener(new HorizontalCalendarListener() {
            @Override
            public void onDateSelected(Date date, int position) {
                //do something
            }
        });

You can also listen to scroll and long press events by overriding each prespective method within HorizontalCalendarListener:

horizontalCalendar.setCalendarListener(new HorizontalCalendarListener() {
            @Override
            public void onDateSelected(Date date, int position) {

            }

            @Override
            public void onCalendarScroll(HorizontalCalendarView calendarView, 
            int dx, int dy) {

            }

            @Override
            public boolean onDateLongClicked(Date date, int position) {
                return true;
            }
        });

Customization

You can customize it directly inside your layout:

<devs.mulham.horizontalcalendar.HorizontalCalendarView
            android:id="@+id/calendarView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            app:textColorNormal="#bababa"
            app:textColorSelected="#FFFF"
            app:selectorColor="#c62828"   // default to colorAccent.
            app:selectedDateBackground="#00ffffff"/>

Or you can do it programmatically in your Activity using the Builder:

HorizontalCalendar horizontalCalendar = new HorizontalCalendar.Builder(this, R.id.calendarView)
                .startDate(startDate.getTime())
                .endDate(endDate.getTime())
                .datesNumberOnScreen(5)   // Number of Dates cells shown on screen (Recommended 5)
                .dayFormat("EEE")     // WeekDay text format
                .dayNumberFormat("dd")  // Date format
                .textColor(Color.LTGRAY, Color.WHITE)    // Text color for none selected Dates, Text color for selected Date.
                .selectedDateBackground(Color.TRANSPARENT)  // Background color of the selected date cell.
                .selectorColor(Color.RED)   // Color of the selection indicator bar (default to colorAccent).
                .build();

Features

  • You can select a specific Date programmatically with the option whether to play the animation or not, with:
horizontalCalendar.selectDate(Date date, boolean immediate); // set immediate to false to ignore animation.
    // or simply
horizontalCalendar.goToday(boolean immediate);
  • Check if two dates' days are equal:
horizontalCalendar.isDatesDaysEquals(Date date1, Date date2);
  • Check if a date is contained in the Calendar:
horizontalCalendar.contains(Date date);

Contributing

Contributions are welcome, feel free to submit a pull request.

License

Copyright 2017 Mulham Raee

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing
permissions and limitations under the License.

No Reply at the moment.
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up