欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Andorid 日历控件库,可左右滑动,显示公历,农历,节假日等功能

程序员文章站 2024-03-06 11:10:13
封面图:  demo效果图   源码目录结构        ...

封面图: 

Andorid 日历控件库,可左右滑动,显示公历,农历,节假日等功能

demo效果图

 Andorid 日历控件库,可左右滑动,显示公历,农历,节假日等功能

源码目录结构        

Andorid 日历控件库,可左右滑动,显示公历,农历,节假日等功能

features

  1. 日历左右滑动.
  2. 显示阳历,农历,节假日和二十四节气
  3. 实现对某月日期的单选或者多选.

使用步骤

gradle dependency

add the library to your project build.gradle

  compile 'com.joybar.calendar:librarycalendar:1.0.4'

sample usage

实现onpagechangelistener和ondateclicklistener接口,如果实现多选,需要实现 ondatecancellistener

 public class mainactivity extends appcompatactivity implements

 calendarviewpagerfragment.onpagechangelistener,

 calendarviewfragment.ondateclicklistener,

 calendarviewfragment.ondatecancellistener {

 

 private textview tv_date;

 private boolean ischoicemodelsingle = false;

 private list<calendardate> mlistdate = new arraylist<>();

 

 @override

 protected void oncreate(bundle savedinstancestate) {

 super.oncreate(savedinstancestate);

 setcontentview(r.layout.activity_main);

 tv_date = (textview) findviewbyid(r.id.tv_date);

 initfragment();

 }

 

 private void initfragment(){

 fragmentmanager fm = getsupportfragmentmanager();

 fragmenttransaction tx = fm.begintransaction();

 // fragment fragment = new calendarviewpagerfragment();

 fragment fragment = calendarviewpagerfragment.newinstance(ischoicemodelsingle);

 tx.replace(r.id.fl_content, fragment);

 tx.commit();

 }

 

 

 @override

 public boolean oncreateoptionsmenu(menu menu) {

 getmenuinflater().inflate(r.menu.menu_im, menu);

 return true;

 }

 @override

 public boolean onoptionsitemselected(menuitem item) {

 switch (item.getitemid()) {

  case r.id.menu_single:

  ischoicemodelsingle = true;

  initfragment();

  break;

  case r.id.menu_multi:

  ischoicemodelsingle = false;

  initfragment();

  break;

  default:

  break;

 }

 return true;

 }

 @override

 public void ondateclick(calendardate calendardate) {

 

 int year = calendardate.getsolar().solaryear;

 int month = calendardate.getsolar().solarmonth;

 int day = calendardate.getsolar().solarday;

 if (ischoicemodelsingle) {

  tv_date.settext(year + "-" + month + "-" + day);

 } else {

  //system.out.println(calendardate.getsolar().solarday);

  mlistdate.add(calendardate);

  tv_date.settext(listtostring(mlistdate));

 }

 

 }

 

 @override

 public void ondatecancel(calendardate calendardate) {

 int count = mlistdate.size();

 for (int i = 0; i < count; i++) {

  calendardate date = mlistdate.get(i);

  if (date.getsolar().solarday == calendardate.getsolar().solarday) {

  mlistdate.remove(i);

  break;

  }

 }

 tv_date.settext(listtostring(mlistdate));

 }

 

 @override

 public void onpagechange(int year, int month) {

 tv_date.settext(year + "-" + month);

 mlistdate.clear();

 }

 

 private static string listtostring(list<calendardate> list) {

 stringbuffer stringbuffer = new stringbuffer();

 for (calendardate date : list) {

  stringbuffer.append(date.getsolar().solaryear + "-" + date.getsolar().solarmonth + "-" + date.getsolar().solarday).append(" ");

 }

 return stringbuffer.tostring();

 }

 

}

单选或者多选的实现代码

 if (ischoicemodelsingle) {

  mgridview.setchoicemode(gridview.choice_mode_single);

 } else {

  mgridview.setchoicemode(gridview.choice_mode_multiple);

 }

 mgridview.setonitemclicklistener(new adapterview.onitemclicklistener() {

  @override

  public void onitemclick(adapterview<?> parent, view view, int position, long id) {

  calendardate calendardate = ((calendargridviewadapter) mgridview.getadapter()).getlistdata().get(position);

  if (ischoicemodelsingle) {

   //单选

   if (finalmlistdatacalendar.get(position).isinthismonth()) {

   ondateclicklistener.ondateclick(calendardate);

   } else {

   mgridview.setitemchecked(position, false);

   }

  } else {

   //多选

   if (finalmlistdatacalendar.get(position).isinthismonth()) {

   // mgridview.getcheckeditemids()

   if(!mgridview.isitemchecked(position)){

    ondatecancellistener.ondatecancel(calendardate);

   } else {

    ondateclicklistener.ondateclick(calendardate);

   }

 

   } else {

   mgridview.setitemchecked(position, false);

   }

 

  }

  }

 });

git地址:https://github.com/myjoybar/android-calendar-view

 以上就是android 日历控件的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!