Skip to content

Commit

Permalink
调整adapter模式
Browse files Browse the repository at this point in the history
  • Loading branch information
yannecer committed Mar 27, 2020
1 parent 3953f86 commit 5e8439f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 2 additions & 0 deletions app/src/main/res/layout/activity_adapter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">


Expand All @@ -14,6 +15,7 @@
<com.necer.calendar.Miui10Calendar
android:id="@+id/miui10Calendar"
android:layout_width="match_parent"
app:showNumberBackground="true"
android:layout_height="match_parent">

<androidx.core.widget.NestedScrollView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class GridCalendarAdapter extends BaseAdapter {


private List<View> viewList;

public GridCalendarAdapter(List<View> viewList) {
this.viewList = viewList;
}
Expand All @@ -38,6 +39,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
int realHeight = parent.getMeasuredHeight() - parent.getPaddingBottom() - parent.getPaddingTop();
AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, realHeight / (viewList.size() / 7));
calendarItemView.setLayoutParams(params);
((CalendarView2) parent).bindView(position, calendarItemView);
return calendarItemView;
}
}
24 changes: 12 additions & 12 deletions ncalendar/src/main/java/com/necer/view/CalendarView2.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.necer.view;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ListAdapter;
Expand Down Expand Up @@ -35,6 +37,7 @@ public class CalendarView2 extends GridView implements ICalendarView {
private CalendarAdapter mCalendarAdapter;
private List<LocalDate> mDateList;
private int mCurrentDistance = -1;
private BaseAdapter gridViewAdapter;


public CalendarView2(Context context, BaseCalendar calendar, LocalDate initialDate, CalendarType calendarType) {
Expand All @@ -61,8 +64,8 @@ public CalendarView2(Context context, BaseCalendar calendar, LocalDate initialDa
viewList.add(calendarItem);
}

ListAdapter adapter = new GridCalendarAdapter(viewList);
setAdapter(adapter);
gridViewAdapter = new GridCalendarAdapter(viewList);
setAdapter(gridViewAdapter);

}

Expand All @@ -86,24 +89,21 @@ private void drawBackground(Canvas canvas, CalendarBackground calendarBackground
}

//刷新view
public void bindView() {
for (int i = 0; i < getChildCount(); i++) {
LocalDate localDate = mDateList.get(i);
View view = getChildAt(i);
public void bindView(int position,View itemView) {
LocalDate localDate = mDateList.get(position);
if (mCalendarHelper.isAvailableDate(localDate)) {
if (mCalendarHelper.isCurrentMonthOrWeek(localDate)) { //当月日期
if (CalendarUtil.isToday(localDate)) { //当天
mCalendarAdapter.onBindToadyView(view, localDate, mCalendarHelper.getAllSelectListDate());
mCalendarAdapter.onBindToadyView(itemView, localDate, mCalendarHelper.getAllSelectListDate());
} else { //不是当天的当月其他日期
mCalendarAdapter.onBindCurrentMonthOrWeekView(view, localDate, mCalendarHelper.getAllSelectListDate());
mCalendarAdapter.onBindCurrentMonthOrWeekView(itemView, localDate, mCalendarHelper.getAllSelectListDate());
}
} else { //不是当月的日期
mCalendarAdapter.onBindLastOrNextMonthView(view, localDate, mCalendarHelper.getAllSelectListDate());
mCalendarAdapter.onBindLastOrNextMonthView(itemView, localDate, mCalendarHelper.getAllSelectListDate());
}
} else { //日期区间之外的日期
mCalendarAdapter.onBindDisableDateView(view, localDate);
mCalendarAdapter.onBindDisableDateView(itemView, localDate);
}
}
}

@Override
Expand Down Expand Up @@ -160,7 +160,7 @@ public List<LocalDate> getCurrPagerDateList() {

@Override
public void notifyCalendarView() {
bindView();
gridViewAdapter.notifyDataSetChanged();
}

//周或者月的第一天
Expand Down

0 comments on commit 5e8439f

Please sign in to comment.