From eeecaec190d16255a0e8ec76ff14555b5082678d Mon Sep 17 00:00:00 2001 From: liewstar <2935437378@qq.com> Date: Sat, 7 Dec 2024 17:25:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E5=91=A8=E6=95=B0=E7=9A=84=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/CQUPT/adapter/CourseAdapter.java | 58 +++++++------------ .../java/com/example/CQUPT/model/Course.java | 6 +- .../example/CQUPT/ui/home/HomeFragment.java | 18 +----- .../example/CQUPT/ui/home/HomeViewModel.java | 9 ++- 4 files changed, 37 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/com/example/CQUPT/adapter/CourseAdapter.java b/app/src/main/java/com/example/CQUPT/adapter/CourseAdapter.java index 573be1b..5471b26 100644 --- a/app/src/main/java/com/example/CQUPT/adapter/CourseAdapter.java +++ b/app/src/main/java/com/example/CQUPT/adapter/CourseAdapter.java @@ -21,11 +21,9 @@ public class CourseAdapter extends RecyclerView.Adapter { private List courses; - private int currentWeek; - public CourseAdapter(List courses, int currentWeek) { + public CourseAdapter(List courses) { this.courses = courses; - this.currentWeek = currentWeek; } public void setCourses(List courses) { @@ -33,11 +31,6 @@ public void setCourses(List courses) { notifyDataSetChanged(); } - public void setCurrentWeek(int currentWeek) { - this.currentWeek = currentWeek; - notifyDataSetChanged(); - } - @NonNull @Override public CourseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { @@ -48,7 +41,7 @@ public CourseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewTy @Override public void onBindViewHolder(@NonNull CourseViewHolder holder, int position) { Course course = courses.get(position); - holder.bind(course, currentWeek); + holder.bind(course); } @Override @@ -76,20 +69,21 @@ public CourseViewHolder(@NonNull View itemView) { currentWeekText = itemView.findViewById(R.id.currentWeekText); } - public void bind(Course course, int currentWeek) { + public void bind(Course course) { Context context = itemView.getContext(); - + // 设置基本信息 courseTimeText.setText(course.getTimeRange()); courseNameText.setText(course.getName()); locationText.setText(course.getLocation()); teacherText.setText(course.getTeacher()); weekRangeText.setText(course.getWeekRange()); - currentWeekText.setText(String.format("第%d周", currentWeek)); + currentWeekText.setText(String.format("第%d周", course.getCurrentWeek())); // 判断课程是否在当前周 - boolean isInCurrentWeek = currentWeek >= course.getStartWeek() && currentWeek <= course.getEndWeek(); - + boolean isInCurrentWeek = course.getCurrentWeek() >= course.getStartWeek() + && course.getCurrentWeek() <= course.getEndWeek(); + // 根据是否在当前周设置卡片样式 if (isInCurrentWeek) { cardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.purple_50)); @@ -100,33 +94,25 @@ public void bind(Course course, int currentWeek) { cardView.setCardBackgroundColor(Color.WHITE); cardView.setStrokeColor(Color.TRANSPARENT); cardView.setStrokeWidth(0); - courseNameText.setTextColor(ContextCompat.getColor(context, android.R.color.black)); + courseNameText.setTextColor(Color.BLACK); } - // 设置点击事件显示详情 - cardView.setOnClickListener(v -> showCourseDetail(context, course, currentWeek)); + // 设置点击事件显示课程详情 + cardView.setOnClickListener(v -> showCourseDetailDialog(context, course)); } - private void showCourseDetail(Context context, Course course, int currentWeek) { - View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_course_detail, null); - - // 设置详情对话框的内容 - TextView courseNameTitle = dialogView.findViewById(R.id.courseNameTitle); - TextView timeText = dialogView.findViewById(R.id.timeText); - TextView weekRangeDetailText = dialogView.findViewById(R.id.weekRangeDetailText); - TextView locationDetailText = dialogView.findViewById(R.id.locationDetailText); - TextView teacherDetailText = dialogView.findViewById(R.id.teacherDetailText); - - courseNameTitle.setText(course.getName()); - timeText.setText(course.getTimeRange()); - weekRangeDetailText.setText(String.format("%s(当前第%d周)", course.getWeekRange(), currentWeek)); - locationDetailText.setText(course.getLocation()); - teacherDetailText.setText(course.getTeacher()); - - // 创建并显示对话框 + private void showCourseDetailDialog(Context context, Course course) { new MaterialAlertDialogBuilder(context) - .setView(dialogView) - .setPositiveButton("关闭", null) + .setTitle(course.getName()) + .setMessage(String.format( + "时间: %s\n位置: %s\n教师: %s\n周数: %s\n当前周: 第%d周", + course.getTimeRange(), + course.getLocation(), + course.getTeacher(), + course.getWeekRange(), + course.getCurrentWeek() + )) + .setPositiveButton("确定", null) .show(); } } diff --git a/app/src/main/java/com/example/CQUPT/model/Course.java b/app/src/main/java/com/example/CQUPT/model/Course.java index 857ecb8..4de05ba 100644 --- a/app/src/main/java/com/example/CQUPT/model/Course.java +++ b/app/src/main/java/com/example/CQUPT/model/Course.java @@ -8,8 +8,9 @@ public class Course { private String teacher; private int startWeek; private int endWeek; + private int currentWeek; - public Course(String name, String startTime, String endTime, String location, String teacher, int startWeek, int endWeek) { + public Course(String name, String startTime, String endTime, String location, String teacher, int startWeek, int endWeek, int currentWeek) { this.name = name; this.startTime = startTime; this.endTime = endTime; @@ -17,6 +18,7 @@ public Course(String name, String startTime, String endTime, String location, St this.teacher = teacher; this.startWeek = startWeek; this.endWeek = endWeek; + this.currentWeek = currentWeek; } public String getName() { @@ -51,6 +53,8 @@ public int getEndWeek() { return endWeek; } + public int getCurrentWeek() { return currentWeek; } + public String getWeekRange() { return String.format("第%d-%d周", startWeek, endWeek); } diff --git a/app/src/main/java/com/example/CQUPT/ui/home/HomeFragment.java b/app/src/main/java/com/example/CQUPT/ui/home/HomeFragment.java index 43b9d3c..2c9ba98 100644 --- a/app/src/main/java/com/example/CQUPT/ui/home/HomeFragment.java +++ b/app/src/main/java/com/example/CQUPT/ui/home/HomeFragment.java @@ -62,16 +62,10 @@ private void initializeFormats() { semesterStartDate.set(2024, Calendar.FEBRUARY, 26); } - private int getCurrentWeek() { - long diff = currentDate.getTimeInMillis() - semesterStartDate.getTimeInMillis(); - int daysDiff = (int) (diff / (24 * 60 * 60 * 1000)); - return (daysDiff / 7) + 1; - } - private void setupRecyclerView() { RecyclerView recyclerView = binding.recyclerCourses; - recyclerView.setLayoutManager(new LinearLayoutManager(requireContext())); - courseAdapter = new CourseAdapter(new ArrayList<>(), getCurrentWeek()); + recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + courseAdapter = new CourseAdapter(new ArrayList<>()); recyclerView.setAdapter(courseAdapter); } @@ -115,13 +109,7 @@ private void showDatePicker() { private void updateDateDisplay() { String dateStr = dateFormat.format(currentDate.getTime()); String weekDayStr = weekDayFormat.format(currentDate.getTime()); - int currentWeek = getCurrentWeek(); - binding.buttonDate.setText(String.format("%s %s (第%d周)", dateStr, weekDayStr, currentWeek)); - - // 更新适配器中的当前周 - if (courseAdapter != null) { - courseAdapter.setCurrentWeek(currentWeek); - } + binding.buttonDate.setText(String.format("%s %s", dateStr, weekDayStr)); } private void loadCoursesForDate(Date date) { diff --git a/app/src/main/java/com/example/CQUPT/ui/home/HomeViewModel.java b/app/src/main/java/com/example/CQUPT/ui/home/HomeViewModel.java index 0203e0e..98b670a 100644 --- a/app/src/main/java/com/example/CQUPT/ui/home/HomeViewModel.java +++ b/app/src/main/java/com/example/CQUPT/ui/home/HomeViewModel.java @@ -43,7 +43,7 @@ public HomeViewModel(Application application) { apiDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()); apiTimeFormat = new SimpleDateFormat("HH:mm:ss", Locale.getDefault()); sharedPreferences = PreferenceManager.getDefaultSharedPreferences(application); - + loadCoursesForDate(new Date()); } @@ -123,6 +123,10 @@ private List filterCoursesForDate(List schedules, Date t int startWeek = weekNums.isEmpty() ? 1 : weekNums.get(0); int endWeek = weekNums.isEmpty() ? 1 : weekNums.get(weekNums.size() - 1); + // 获取当前周 + int currentWeek = schedule.getWeekNum(); + + // 创建课程对象 Course course = new Course( schedule.getTitle(), startTime, @@ -130,7 +134,8 @@ private List filterCoursesForDate(List schedules, Date t schedule.getLocation(), schedule.getData().getTeacherName(), startWeek, - endWeek + endWeek, + currentWeek ); courses.add(course); }