赞
踩
package com.hzcy.doctor.view; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.hzcy.doctor.R; import com.hzcy.doctor.hospital.bean.PlanBean; import org.w3c.dom.Text; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 新日历视图 * * @author zhangpenghui * @date 2022/11/10 */ public class NewCalendarView extends LinearLayout { private ImageView lastTv, nextTv; private TextView dateTv; private GridView calendarGv; private Calendar calendar = Calendar.getInstance(); //日历控件初始化 public NewCalendarListener listener; CalendarAdapter calendarAdapter; //重写三个构造方法 public NewCalendarView(Context context) { super(context); } public NewCalendarView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initControl(context); //绑定控件 } public NewCalendarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initControl(context); //绑定控件 } private void initControl(Context context) { calendar.setFirstDayOfWeek(Calendar.MONDAY); bindControl(context); //绑定控件 bindControlEvent(); //绑定控件事件 } //绑定控件事件方法 private void bindControlEvent() { renderCalendar(); //“下一月”点击事件 nextTv.setOnClickListener(v -> { calendar.add(Calendar.MONTH, +1); //月份+1 renderCalendar(); if (listener != null) { Date date = new Date(); int month = getMonth(); listener.onNextMonth(month - date.getMonth()); } }); //“上一个”点击事件 lastTv.setOnClickListener(v -> { calendar.add(Calendar.MONTH, -1); //月份-1 renderCalendar(); if (listener != null) { Date date = new Date(); int month = getMonth(); listener.onNextMonth(month - date.getMonth()); } }); } private int getMonth() { int month = calendar.get(Calendar.MONTH); return month; } private void renderCalendar() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月"); //日期格式化 dateTv.setText(sdf.format(calendar.getTime())); //设置月份 ArrayList<Date> cells = new ArrayList<>(); Calendar calendar1 = (Calendar) calendar.clone(); //克隆日历对象 calendar1.setFirstDayOfWeek(Calendar.MONDAY); calendar1.set(Calendar.DAY_OF_MONTH, 1); //置于当月第一天; int prevDays = calendar1.get(Calendar.DAY_OF_WEEK) - 2; //获取上个月最后一天是星期几 calendar1.add(Calendar.DAY_OF_MONTH, -prevDays); //第一天 //修改星期排列后 每月第一天可能不是1号 日期需要前推一周 int day = calendar1.get(Calendar.DAY_OF_MONTH); int monthO = calendar1.get(Calendar.MONTH); int monthN = calendar.get(Calendar.MONTH); if (monthO == monthN && day > 1) { calendar1.add(Calendar.DAY_OF_MONTH, -7); } // 获取每月有几周 int actualMaximum = calendar.getActualMaximum(Calendar.WEEK_OF_MONTH); int maxCount = actualMaximum * 7; //设置每个月最大天数 //循环存入集合中 while (cells.size() < maxCount) { cells.add(calendar1.getTime()); calendar1.add(Calendar.DAY_OF_MONTH, 1); //日期+1 } calendarAdapter = new CalendarAdapter(getContext(), cells); //设置适配器 calendarGv.setAdapter(calendarAdapter); //适配器长按事件 calendarGv.setOnItemClickListener((parent, view, position, id) -> { if (listener == null) { } else { //获取长按的位置,传入onItemClick方法中 Date atPosition = (Date) parent.getItemAtPosition(position); listener.onItemClick(atPosition); calendarAdapter.selectDate(atPosition); } }); } public void setListener(NewCalendarListener listener) { this.listener = listener; } //适配器 private class CalendarAdapter extends ArrayAdapter<Date> { LayoutInflater layoutInflater; Date selDate = null; public CalendarAdapter(@NonNull Context context, ArrayList<Date> days) { super(context, R.layout.item_layout, days); layoutInflater = LayoutInflater.from(context); } @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { Date date = getItem(position); ViewHolder viewHolder; if (convertView == null) { //初始化绑定 convertView = layoutInflater.inflate(R.layout.item_layout, parent, false); viewHolder = new ViewHolder(); viewHolder.itemTv = convertView.findViewById(R.id.itemTv); viewHolder.itemState = convertView.findViewById(R.id.itemState); viewHolder.layView = convertView.findViewById(R.id.lay); convertView.setTag(viewHolder); } viewHolder = (ViewHolder) convertView.getTag(); int day = date.getDate(); viewHolder.itemTv.setText(String.valueOf(day)); //赋值 Date now = new Date(); if (selDate != null) { now = selDate; } Boolean isTheSameMonth = false; //是否与当前月份相同 //判断显示的日期月份与当前月份相同 // if (date.getMonth() == now.getMonth()) { //月份相同 // isTheSameMonth = true; // } if (date.getMonth() == getMonth()) { //月份相同 isTheSameMonth = true; } //若显示的日期月份与当前月份相同,则设置字体颜色是黑色 if (isTheSameMonth) { viewHolder.itemTv.setTextColor(Color.parseColor("#333333")); } else { viewHolder.itemTv.setTextColor(Color.parseColor("#999999")); } //设置当前日期字体为红色 if (now.getDate() == date.getDate() && now.getMonth() == date.getMonth() && now.getYear() == date.getYear()) { // viewHolder.itemTv.setTextColor(Color.parseColor("#ff0000")); viewHolder.layView.setBackground(getContext().getResources().getDrawable(R.drawable.bg_corner_4_20_p)); viewHolder.itemState.setTextColor(Color.WHITE); } else { viewHolder.layView.setBackgroundColor(Color.WHITE); viewHolder.itemState.setTextColor(getContext().getResources().getColor(R.color.theme_color)); } String ds = new SimpleDateFormat("yyyy-MM-dd").format(date); // System.out.println(ds); // PlanBean planBean = mapList.get(ds); if (mapList.containsKey(ds)) { viewHolder.itemState.setText("出诊"); } else { viewHolder.itemState.setText(""); } return convertView; } public void selectDate(Date date) { selDate = date; notifyDataSetChanged(); } class ViewHolder { TextView itemTv; TextView itemState; View layView; } } //长按事件接口 public interface NewCalendarListener { void onItemClick(Date date); void onNextMonth(int next); } //控件绑定方法 private void bindControl(Context context) { LayoutInflater inflater = LayoutInflater.from(context); inflater.inflate(R.layout.calendar_layout, this); lastTv = findViewById(R.id.lastTv); nextTv = findViewById(R.id.nextTv); dateTv = findViewById(R.id.dateTv); calendarGv = findViewById(R.id.calendarGv); } Map<String, PlanBean> mapList = new HashMap<>(); public void showListData(List<PlanBean> ls) { mapList.clear(); for (PlanBean bean : ls) { // System.out.println(bean.getDate()); mapList.put(bean.getDate(), bean); } calendarAdapter.notifyDataSetChanged(); } public PlanBean getPostionBean(Date date) { String ds = new SimpleDateFormat("yyyy-MM-dd").format(date); return mapList.get(ds); } // 设置选择今天高亮 public void setSelect() { calendarAdapter.selectDate(new Date()); } }
package com.hzcy.doctor.hospital.bean
import com.hzcy.doctor.net.response.PlanShiftsListRes
class PlanBean {
var date: String? = ""
var week: String? = ""
var day: Int? = 0
var month: Int? = 0
var year: Int? = 0
var shiftIds: List<String>? = ArrayList()
var shifts: List<PlanShiftsListRes>? = ArrayList()
}
calendar_layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:padding="@dimen/d20dp"> <androidx.appcompat.widget.LinearLayoutCompat android:id="@+id/titleRl" android:layout_width="wrap_content" android:layout_height="@dimen/d20dp" android:gravity="center_vertical"> <ImageView android:id="@+id/lastTv" android:layout_width="@dimen/d40dp" android:layout_height="@dimen/d20dp" android:gravity="center" android:src="@mipmap/ic_calendar_left" /> <TextView android:id="@+id/dateTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:gravity="center" android:text="2022年11月" android:textColor="@color/color_333333" android:textSize="@dimen/d14sp" android:textStyle="bold" /> <ImageView android:id="@+id/nextTv" android:layout_width="@dimen/d40dp" android:layout_height="@dimen/d20dp" android:gravity="center" android:src="@mipmap/ic_calendar_right" /> </androidx.appcompat.widget.LinearLayoutCompat> <LinearLayout android:id="@+id/weekLl" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/d20dp" android:orientation="horizontal"> <TextView android:id="@+id/Tv1" android:layout_width="0dp" android:layout_height="30dp" android:layout_weight="1" android:gravity="center" android:text="周一" android:textColor="@color/color_333333" android:textSize="@dimen/d12sp" /> <TextView android:id="@+id/Tv2" android:layout_width="0dp" android:layout_height="30dp" android:layout_weight="1" android:gravity="center" android:text="周二" android:textColor="@color/color_333333" android:textSize="@dimen/d12sp" /> <TextView android:id="@+id/Tv3" android:layout_width="0dp" android:layout_height="30dp" android:layout_weight="1" android:gravity="center" android:text="周三" android:textColor="@color/color_333333" android:textSize="@dimen/d12sp" /> <TextView android:id="@+id/Tv4" android:layout_width="0dp" android:layout_height="30dp" android:layout_weight="1" android:gravity="center" android:text="周四" android:textColor="@color/color_333333" android:textSize="@dimen/d12sp" /> <TextView android:id="@+id/Tv5" android:layout_width="0dp" android:layout_height="30dp" android:layout_weight="1" android:gravity="center" android:text="周五" android:textColor="@color/color_333333" android:textSize="@dimen/d12sp" /> <TextView android:id="@+id/Tv6" android:layout_width="0dp" android:layout_height="30dp" android:layout_weight="1" android:gravity="center" android:text="周六" android:textColor="@color/color_333333" android:textSize="@dimen/d12sp" /> <TextView android:id="@+id/Tv7" android:layout_width="0dp" android:layout_height="30dp" android:layout_weight="1" android:gravity="center" android:text="周日" android:textColor="@color/color_333333" android:textSize="@dimen/d12sp" /> </LinearLayout> <GridView android:id="@+id/calendarGv" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:numColumns="7" /> </LinearLayout>
item_layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/lay" android:layout_width="@dimen/d40dp" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/itemTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/d8dp" android:gravity="center" android:text="1" android:textColor="@color/color_999999" android:textSize="@dimen/d14sp" /> <TextView android:id="@+id/itemState" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/d5dp" android:layout_marginBottom="@dimen/d5dp" android:text="出诊" android:textColor="@color/theme_color" android:textSize="@dimen/d12sp" /> </LinearLayout>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。