先来张效果图
之前项目中用到的日历控件都是在网上随便找了个改改用,每次该都挺花时间的,而且根据项目需求不一样改动也比较大,这次项目中用到了就决定自己写个。
看到这个是不是就知道怎么写的了,哈哈哈!继承的LinearLayout 然后addView()搞定!
下面做的就是把日历数据显示出来,这里用的RecycleView!
不管了下面开始粘代码了
/**
* Author: zyc
* Date: 2018/8/7
* Description:
*/
public class CalendarView extends LinearLayout implements View.OnClickListener {
private Contextm Context;
private AdapterCalenderad apterCalender;
private TextView tvTime;
private Calendar calendar;
private String currentDay;//当天
private String currentMonth;//当前显示月份
public CalendarView(Context context) {
this(context,null);
}
public CalendarView(Context context,@Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public CalendarView(Context context,@Nullable AttributeSet attrs,int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
View view = View.inflate(context, R.layout.calendar,null);
initView(view);
this.addView(view);
}
private void initView(View view) {
RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
tvTime = view.findViewById(R.id.tv_time);
ImageView ivLeft = view.findViewById(R.id.iv_left);
ImageView ivRight = view.findViewById(R.id.iv_right);
GridLayoutManager gridLayoutManager =new GridLayoutManager(mContext,7);
gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(gridLayoutManager);
Date date =new Date();
currentDay = getDay(date);
currentMonth = getMonth(date);//"yyyy-MM"
calendar = Calendar.getInstance();
calendar.setTime(date);
List list = getBeanData(calendar);
adapterCalender =new AdapterCalender(mContext, list, R.layout.item_calender);
recyclerView.setAdapter(adapterCalender);
ivLeft.setOnClickListener(this);
ivRight.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_left:
calendar.setTime(getDate());
calendar.add(Calendar.MONTH, -1);
currentMonth = getMonth(calendar.getTime());
adapterCalender.update(getBeanData(calendar));
break;
case R.id.iv_right:
calendar.setTime(getDate());
calendar.add(Calendar.MONTH,1);
currentMonth = getMonth(calendar.getTime());
adapterCalender.update(getBeanData(calendar));
break;
}
}
@NonNull
private List getBeanData(Calendar calendar) {
List list =new ArrayList<>();
tvTime.setText(currentMonth);
//设置到当月的第一天
calendar.set(Calendar.DAY_OF_MONTH,1);
//获取当月第一天在这周第几天
int dayInWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (dayInWeek ==1) {
calendar.add(Calendar.DAY_OF_MONTH, -7);
}else {
calendar.add(Calendar.DAY_OF_MONTH,1 - dayInWeek);
}
for (int i =0; i <42; i++) {
int day = calendar.get(Calendar.DAY_OF_MONTH);
BeanDate beanData =new BeanDate();
beanData.day = day;
beanData.date = calendar.getTime();
list.add(beanData);
calendar.add(Calendar.DAY_OF_MONTH,1);
}
return list;
}
class AdapterCalenderextends CommonRecycleAdapter {
private HashSetnormalSet =new HashSet();
private HashSetabNormalSet =new HashSet();
public AdapterCalender(Context context, List dataList,int layoutId) {
super(context, dataList, layoutId);
normalSet.add("2018-08-01");
normalSet.add("2018-08-03");
normalSet.add("2018-08-04");
normalSet.add("2018-08-05");
normalSet.add("2018-08-06");
normalSet.add("2018-08-07");
normalSet.add("2018-07-31");
normalSet.add("2018-07-29");
abNormalSet.add("2018-08-02");
abNormalSet.add("2018-07-30");
}
@Override
public void bindData(CommonViewHolder holder, BeanDate data,int position) {
holder.setText(R.id.tv_day, String.valueOf(data.day));
TextView tvDay = holder.itemView.findViewById(R.id.tv_day);
//不是当月日期背景浅灰色
if (!currentMonth.equals(getMonth(data.date))) {
tvDay.setTextColor(getResources().getColor(R.color.colorGrayLight));
}else {
tvDay.setTextColor(getResources().getColor(R.color.colorBlackLight));
}
tvDay.setBackgroundResource(R.drawable.calender_boder);
String day = getDay(data.date);
//当天
if (currentDay.equals(day)) {
tvDay.setBackgroundResource(R.drawable.station_bg_number);
tvDay.setTextColor(getResources().getColor(R.color.colorWhite));
}
//正常签到
if (normalSet.contains(day)) {
tvDay.setBackgroundResource(R.drawable.station_sign_normal);
}
//脱岗
if (abNormalSet.contains(day)) {
tvDay.setBackgroundResource(R.drawable.station_sign_abnormal);
tvDay.setTextColor(getResources().getColor(R.color.colorWhite));
}
}
}
class BeanDate {
public Datedate;//对应当月第几天的Date
public int day;//当月的第几天
}
/**
* 将date转string
*
* @param date
* @return yyyy-MM-dd
*/
private String getDay(Date date) {
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
String time = simpleDateFormat.format(date);
return time;
}
/**
* @return yyyy-MM
*/
private Date getDate() {
try {
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM", Locale.CHINESE);
return sdf.parse(tvTime.getText().toString());
}catch (ParseException e) {
e.printStackTrace();
return new Date();
}
}
/**
* @param date
* @return yyyy-MM
*/
private String getMonth(Date date) {
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM");
String time = simpleDateFormat.format(date);
return time;
}
}