The system has a calendar that works, but does not meet the needs of some projects, so you need to customize the layout, look at many custom view on the Web to complete the custom calendar,
Admission level is not enough, can not control, so use the GridView to achieve.
Effect
Calendarutils: (Get the data array for the current date)
Package Kyle.com.zujian;
Import java.util.ArrayList;
Import Java.util.Calendar;
Import java.util.List;
/**
* Created by Kyle on 2016/8/12 0012.
* Get the date of a certain month, the first day of the interface is Sunday
* Pass the year and month, get the corresponding date and the first day is the week
*/
public class Calendarutils {
private static int first_day_of_week;//first day
private static Calendar calendar;//Calendar
public static list<dateentity> getDate (int-year, int month) {
if (year <= 0 | | Month < 1 | | month > 12) {
Return to New arraylist<> ();//The year and month do not conform to the rule, returns empty list
}
list<dateentity> dates = new arraylist<> ();
Calendar = Calendar.getinstance ();
Calendar.set (Calendar.year, year);/set years
Calendar.set (Calendar.month, month-1);/Set Month
Calendar.set (Calendar.day_of_month, 1);/Set first day
First_day_of_week = Calendar.get (calendar.day_of_week)-1;//get first day is week 0: Sunday 6 Saturday
for (int i = 0; i < First_day_of_week i++) {//The assumption is skipped in Sunday, if not Sunday, add the corresponding number of blank item
dateentity entity = new dateentity ();
Dates.add (entity);
}
int count = Checkdays (year, month);//Get the number of days of the current month
for (int i = 0; i < count; i++) {
Dateentity entity2 = new Dateentity (year, month, i + 1);
Dates.add (Entity2);
}
return dates;
}
/***
* Determine how many days are there in the current year
*
* @param year
* @param month
* @return
*/
private static int checkdays (int year, int month) {
if (Year < 1 | | month > | | Month < 1) {
return 0;
}
Switch (month) {
Case 2:
if (year% 4 = 0) {
return 29;
} else {
return 28;
}
Case 1:
Case 3:
Case 5:
Case 7:
Case 8:
Case 10:
Case 12:
return 31;
Case 4:
Case 6:
Case 9:
Case 11:
return 30;
}
return 0;
}
}
Entity class: Dateentity
Package Kyle.com.zujian;
/**
* Created by the Administrator on 2016/8/9 0009.
*/
public class Dateentity {
private int year;
private int month;
private int day;
@Override
Public String toString () {
Return "dateentity{" +
"Year=" + year +
", month=" + month +
", day=" + Day +
'}';
}
Public dateentity () {
}
Public dateentity (int-year, int month, int day) {
This.year = year;
This.month = month;
This.day = day;
}
public int getyear () {
return to year;
}
public void setyear (int year) {
This.year = year;
}
public int getmonth () {
Return month;
}
public void setmonth (int month) {
This.month = month;
}
public int getday () {
Return day;
}
public void Setday (int day) {
This.day = day;
}
}
Calendargridview: (custom GridView, transfer code to here for easy invocation.)
Package Kyle.com.zujian;
Import Android.content.Context;
Import Android.support.v4.content.ContextCompat;
Import Android.util.AttributeSet;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.GridView;
Import Android.widget.LinearLayout;
Import Android.widget.TextView;
Import Android.widget.Toast;
Import java.util.ArrayList;
Import Java.util.Calendar;
Import java.util.List;
/**
* Created by Kyle on 2016/8/15 0015.
* Custom Calendar GridView
*/
public class Calendargridview extends GridView {
Private context Mcontext;
Private list<dateentity> mlist = new arraylist<> ();
Private Myadapter Madapter;
private Calendar Calendar;
/***
* Today
*/
private int now_year;//when the year before
private int now_month;//Current Month
private int now_day;//Current day
Public Calendargridview {
Super (context, NULL);
Mcontext = context;
}
Public Calendargridview (context context, AttributeSet Attrs) {
Super (context, attrs);
Mcontext = context;
Calendar = Calendar.getinstance ();//Get Calendars instance
Now_year = Calendar.get (calendar.year);//When the year before last
Now_month = Calendar.get (calendar.month) + 1;//Current Month
Now_day = Calendar.get (calendar.day_of_month);//Current day
Madapter = new Myadapter ()//instantiate and set adapter
Setadapter (Madapter);
}
public void Init (int. year, int month) {
if (Year < 1 | | Month < 1 | | month > 12) {
Toast.maketext (Mcontext, "Year or month error!", Toast.length_short). Show ();
Return
}
Mlist.clear ();
For (Dateentity Entity:CalendarUtils.getDate (year, month)) {
Mlist.add (entity);
}
Madapter.notifydatasetchanged ();
}
Private class Myadapter extends Baseadapter {
@Override
public int GetCount () {
return Mlist.size ();
}
@Override
Public Object getitem (int position) {
return Mlist.get (position);
}
@Override
public long getitemid (int position) {
return position;
}
@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
Myholder Holder;
if (Convertview = = null) {
Convertview = View.inflate (mcontext, R.layout.item_grid, NULL);
Holder = new Myholder ();
Holder.llmain = (linearlayout) Convertview.findviewbyid (R.id.ll_main);
Holder.tvdate = (TextView) Convertview.findviewbyid (r.id.tv_date);
Holder.tvcount = (TextView) Convertview.findviewbyid (R.id.tv_count);
Convertview.settag (holder);
} else {
Holder = (myholder) convertview.gettag ();
}
int year = Mlist.get (position). getyear ();
int month = Mlist.get (position). GetMonth ();
int day = Mlist.get (position). Getday ();
if (year = = now_year && month = now_month && Day = = Now_day) {
Holder.llMain.setBackgroundResource (r.color.red);
Holder.tvDate.setTextColor (Contextcompat.getcolor (Mcontext, r.color.white));
Holder.tvCount.setTextColor (Contextcompat.getcolor (Mcontext, r.color.white));
} else {
Holder.llMain.setBackgroundResource (R.color.white);
Holder.tvDate.setTextColor (Contextcompat.getcolor (Mcontext, r.color.black));
Holder.tvCount.setTextColor (Contextcompat.getcolor (Mcontext, r.color.black));
}
if (day!= 0) {
Holder.tvDate.setText (Mlist.get (position). Getday () + "");
} else {
Holder.tvDate.setText ("");
}
return convertview;
}
}
Private class Myholder {
Private LinearLayout Llmain;
Private TextView tvdate;
Private TextView Tvcount;
}
}
This is called in the activity:
Calendargridview mgv= (Calendargridview) Findviewbyid (R.ID.GV);
Mgv.init (2016,8);
Of course, layout and entity classes can be written on demand, and if you want real calendar functionality, you can use viewpager+fragment to implement the