xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/beforeData1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:padding="2dp"
android:src="@mipmap/l_arrow" />
<TextView
android:id="@+id/time1"
android:layout_width="120dp"
android:layout_height="30dp"
android:background="@drawable/bg_edit_3cir"
android:gravity="center"
android:text="2022年10月"
android:textColor="#ff040000"
android:textSize="@dimen/text_18" />
<ImageView
android:id="@+id/nextData1"
android:layout_width="30dp"
android:layout_height="30dp"
android:padding="2dp"
android:src="@mipmap/r_arrow" />
</LinearLayout>
<LinearLayout
android:id="@+id/lin_date_layout"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="一"
android:textColor="@color/black"
android:textSize="16sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="二"
android:textColor="@color/black"
android:textSize="16sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="三"
android:textColor="@color/black"
android:textSize="16sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="四"
android:textColor="@color/black"
android:textSize="16sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="五"
android:textColor="@color/black"
android:textSize="16sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="六"
android:textColor="@color/black"
android:textSize="16sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="日"
android:textColor="@color/black"
android:textSize="16sp" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_cal"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginBottom="70dp"
android:nestedScrollingEnabled="true"
android:orientation="vertical"
android:scrollbars="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</LinearLayout>
java:
private int year;
private int month;
private List<DayInfo> dayInfos = new ArrayList<>();
private CalenderDateAdapter myAttendanceDateAdapter;
private void setupDate() {
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1;
TextView time1 = findViewById(R.id.time1);
ImageView beforeView1 = findViewById(R.id.beforeData1);
ImageView nextDate1 = findViewById(R.id.nextData1);
RecyclerView rv_cal = findViewById(R.id.rv_cal);
myAttendanceDateAdapter = new CalenderDateAdapter(context, dayInfos);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 7);
rv_cal.setLayoutManager(gridLayoutManager);
rv_cal.setAdapter(myAttendanceDateAdapter);
initDate();
beforeView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (month == 1) {
month = 12;
year--;
} else {
month--;
}
time1.setText(year + "年" + month + "月");
initDate();
}
});
nextDate1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (month == 12) {
month = 1;
year++;
} else {
month++;
}
time1.setText(year + "年" + month + "月");
initDate();
}
});
}
private void initDate() {
int weekDay = DateUtils.getWeekDay(year + "-" + month + "-1");
int totalDays = DateUtils.getMonthDays(year, month);
dayInfos.clear();
for (int i = 0; i < weekDay - 1; i++) {
dayInfos.add(null);
}
for (int i = 0; i < totalDays; i++) {
DayInfo dayInfo = new DayInfo();
dayInfo.setDay((i + 1) + "");
dayInfo.setTime(year + "-" + month + "-" + (i + 1));
if (i % 4 == 0) {
List<String> tags = new ArrayList<>();
tags.add("1");
if (i % 6 == 0) {
tags.add("2");
}
if (i % 8 == 0) {
tags.add("3");
}
dayInfo.setTags(tags);
}
dayInfos.add(dayInfo);
}
if (m