先來張效果圖

之前項目中用到的日歷控件都是在網(wǎng)上隨便找了個改改用,每次該都挺花時間的,而且根據(jù)項目需求不一樣改動也比較大,這次項目中用到了就決定自己寫個。

看到這個是不是就知道怎么寫的了,哈哈哈!繼承的LinearLayout 然后addView()搞定!
下面做的就是把日歷數(shù)據(jù)顯示出來,這里用的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);
//設(shè)置到當月的第一天
? ? ? ? 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;//對應(yīng)當月第幾天的Date
? ? ? ? public int day;//當月的第幾天
? ? }
/**
* 將date轉(zhuǎn)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;
}
}