public class ScreenUtils { public static int getScreenWidth(Context context) { DisplayMetrics localDisplayMetrics = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics); return localDisplayMetrics.widthPixels; }}
DateBean:
public class DateBean { private int week; private boolean sign; //0 上月 1本月 2下月 private int month; private boolean isFlag; public boolean isFlag() { return isFlag; } public void setFlag(boolean flag) { isFlag = flag; } public int getWeek() { return week; } public void setWeek(int week) { this.week = week; } public boolean isSign() { return sign; } public void setSign(boolean sign) { this.sign = sign; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public DateBean(int week, boolean sign, int month) { this.week = week; this.sign = sign; this.month = month; }}
适配器:DateAdapter
public class DateAdapter extends CommonRecyclerAdapter { private static final String TAG = "DateAdapter"; private Integer nowDay; private int year; private int month; public DateAdapter(Context context, List list) { super(context, R.layout.item_date, list); } public void setNowDay(int year, int month, int nowDay) { this.nowDay = nowDay; this.year = year; this.month = month; notifyDataSetChanged(); } @Override public void convert(RecyclerHolder holder, DateBean item, int position) { TextView number = holder.getView(R.id.item_number); number.setText(item.getWeek() + ""); //当前年月等于切换年月时才选中当天 if (position == nowDay) { String date = year + "-" + month; if (date.equals(DataUtils.timeInMillsTrasToDate(1))) { number.setBackgroundResource(R.drawable.date_unsel_shape); number.setTextColor(Color.WHITE); }else { number.setTextColor(Color.parseColor("#333333")); } } else { number.setBackgroundResource(0); number.setTextColor(Color.parseColor("#333333")); if (item.getMonth() == 0 || item.getMonth() == 2) { number.setTextColor(Color.parseColor("#CDCDCD")); } else { number.setTextColor(Color.parseColor("#333333")); } } //点击事件 number.setOnClickListener(view -> { //本月可以点击 int nowYear = Integer.parseInt(DataUtils.timeInMillsTrasToDate(2)); int nowMonth = Integer.parseInt(DataUtils.timeInMillsTrasToDate(3)); //只有在今天之后的日期才可以选中 if (year == nowYear) { if (item.getMonth() == 1 && month == nowMonth && position > nowDay) { onClick(item, number); } else if (month > nowMonth) { onClick(item, number); } } else if (year > nowYear) { onClick(item, number); } }); } private void onClick(DateBean item, TextView number) { if (item.isFlag()) { item.setFlag(false); number.setBackgroundResource(0); number.setTextColor(Color.parseColor("#333333")); } else { item.setFlag(true); number.setBackgroundResource(R.drawable.date_sel_shape); number.setTextColor(Color.WHITE); } }}
注意:CommonRecyclerAdapter之前写过,这里不再重复
数据源:DataUtils
public class DataUtils { private static final String TAG = "DataUtils"; /** * 日历 */ public static List getCalendar(int currentYear, int currentMonth) { //实例化集合 List list = new ArrayList<>(); Calendar c = Calendar.getInstance(); c.clear(); /** * 处理上个月月末 */ if (currentMonth - 1 == 0) { c.set(Calendar.YEAR, currentYear - 1); c.set(Calendar.MONTH, 11); } else { c.set(Calendar.YEAR, currentYear); c.set(Calendar.MONTH, (currentMonth - 2)); } int last_sumDays = c.getActualMaximum(Calendar.DAY_OF_MONTH); c.set(Calendar.DATE, last_sumDays); //得到一号星期几 int weekDay = c.get(Calendar.DAY_OF_WEEK); if (weekDay < 7) { for (int i = weekDay - 1; i >= 0; i--) { Integer date = new Integer(last_sumDays - i); list.add(new DateBean(date, true, 0)); } } /** * 处理当前月 */ c.clear(); c.set(Calendar.YEAR, currentYear); c.set(Calendar.MONTH, currentMonth - 1); int currentsumDays = c.getActualMaximum(Calendar.DAY_OF_MONTH); for (int i = 1; i <= currentsumDays; i++) { Integer date = new Integer(i); list.add(new DateBean(date, true, 1)); } /** * 处理下个月月初 */ c.clear(); if (currentMonth == 12) { c.set(Calendar.YEAR, currentYear + 1); c.set(Calendar.MONTH, 0); } else { c.set(Calendar.YEAR, currentYear); c.set(Calendar.MONTH, currentMonth); } c.set(Calendar.DATE, 1); int currentWeekDay = c.get(Calendar.DAY_OF_WEEK); if (currentWeekDay > 2 && currentWeekDay < 8) { for (int i = 1; i <= 8 - currentWeekDay; i++) { list.add(new DateBean(i, true, 2)); } } return list; } /** * 获取上个月大小 */ public static Integer getLastMonth(int currentYear, int currentMonth) { //实例化集合 List list = new ArrayList<>(); Calendar c = Calendar.getInstance(); c.clear(); /** * 处理上个月月末 */ if (currentMonth - 1 == 0) { c.set(Calendar.YEAR, currentYear - 1); c.set(Calendar.MONTH, 11); } else { c.set(Calendar.YEAR, currentYear); c.set(Calendar.MONTH, (currentMonth - 2)); } int last_sumDays = c.getActualMaximum(Calendar.DAY_OF_MONTH); c.set(Calendar.DATE, last_sumDays); //得到一号星期几 int weekDay = c.get(Calendar.DAY_OF_WEEK); if (weekDay < 7) { for (int i = weekDay - 1; i >= 0; i--) { list.add(i); } } Log.e(TAG, "getLastMonth: " + Integer.parseInt(timeInMillsTrasToDate(0))); return list.size() + Integer.parseInt(timeInMillsTrasToDate(0)) - 1; } /** * list转string字符串 */ public static String returnList(List list) { if (null == list && list.size() == 0) { return ""; } else { //去除空格 String str = String.valueOf(list).replaceAll(" ", ""); return str.substring(1, str.length() - 1); } } /** * 时间转换 */ @TargetApi(Build.VERSION_CODES.N) public static String timeInMillsTrasToDate(int formatType) { DateFormat formatter = null; switch (formatType) { case 0: formatter = new SimpleDateFormat("dd"); break; case 1: formatter = new SimpleDateFormat("yyyy-MM"); break; case 2: formatter = new SimpleDateFormat("yyyy"); break; case 3: formatter = new SimpleDateFormat("MM"); break; } Calendar calendar = Calendar.getInstance(); return formatter.format(calendar.getTime()); }}
使用:DateActivity
public class DateActivity extends AppCompatActivity { private Button openDate; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_date); openDate = this.findViewById(R.id.open_date); openDate.setOnClickListener(view -> { showDateDialog(); }); } private void showDateDialog() { int year = Integer.parseInt(DataUtils.timeInMillsTrasToDate(2)); int month = Integer.parseInt(DataUtils.timeInMillsTrasToDate(3)); new SelectDateDialog().builder(this, year, month) .setListener(selectDate -> openDate.setText(selectDate)).show(); }}