Skip to content

Commit

Permalink
更改dayview的绘制机制 已提升效率
Browse files Browse the repository at this point in the history
  • Loading branch information
骆大峰 committed Jul 5, 2017
1 parent c6bb0ba commit 062ac4e
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
Expand Down Expand Up @@ -38,13 +39,15 @@ public boolean onLayoutChild(CoordinatorLayout parent, RecyclerView child, int l
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, RecyclerView child,
View directTargetChild, View target, int nestedScrollAxes) {
boolean isVertical = (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) child.getLayoutManager();
if(linearLayoutManager.findFirstCompletelyVisibleItemPosition() > 0) {
return false;
}

boolean isVertical = (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
int firstRowVerticalPosition =
(child == null || child.getChildCount() == 0) ? 0 : child.getChildAt(0).getTop();

boolean recycleviewTopStatus = firstRowVerticalPosition >= 0;

return isVertical && (recycleviewTopStatus || !Utils.isScrollToBottom()) && child == directTargetChild;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;

import com.ldf.calendar.Const;
import com.ldf.calendar.Utils;
import com.ldf.calendar.interf.IDayRenderer;
import com.ldf.calendar.interf.OnSelectDateListener;
import com.ldf.calendar.model.CalendarDate;
import com.ldf.calendar.view.Calendar;
import com.ldf.calendar.view.Day;
import com.ldf.calendar.view.Week;

/**
Expand All @@ -18,6 +21,7 @@ public class CalendarRenderer {
private Week weeks[] = new Week[Const.TOTAL_ROW]; // 行数组,每个元素代表一行
private Calendar calendar;
private CalendarAttr attr;
private IDayRenderer dayRenderer;
private Context context;
private OnSelectDateListener onSelectDateListener; // 单元格点击回调事件
private CalendarDate seedDate; //种子日期
Expand All @@ -32,8 +36,13 @@ public CalendarRenderer(Calendar calendar , CalendarAttr attr , Context context)

public void draw(Canvas canvas) {
for (int row = 0; row < Const.TOTAL_ROW; row++) {
if (weeks[row] != null)
weeks[row].drawRow(canvas);
if (weeks[row] != null) {
for (int col = 0; col < Const.TOTAL_COL; col ++) {
if (weeks[row].days[col] != null) {
dayRenderer.drawDay(canvas , weeks[row].days[col]);
}
}
}
}
}

Expand All @@ -43,7 +52,7 @@ public void onClickDate(int col, int row) {
if (weeks[row] != null) {
if(attr.getCalendarType() == CalendarAttr.CalendayType.MONTH) {
if(weeks[row].days[col].getState() == State.CURRENT_MONTH){
weeks[row].days[col].refreshState(State.SELECT);
weeks[row].days[col].setState(State.SELECT);
selectedDate = weeks[row].days[col].getDate();
CalendarViewAdapter.saveDate(selectedDate);
onSelectDateListener.onSelectDate(selectedDate);
Expand All @@ -60,7 +69,7 @@ public void onClickDate(int col, int row) {
onSelectDateListener.onSelectDate(selectedDate);
}
} else {
weeks[row].days[col].refreshState(State.SELECT);
weeks[row].days[col].setState(State.SELECT);
selectedDate = weeks[row].days[col].getDate();
CalendarViewAdapter.saveDate(selectedDate);
onSelectDateListener.onSelectDate(selectedDate);
Expand All @@ -79,10 +88,23 @@ public void updateWeek(int rowIndex) {
int day = currentWeekLastDay.day;
for (int i = Const.TOTAL_COL - 1; i >= 0 ; i --) {
CalendarDate date = currentWeekLastDay.modifyDay(day);
if (date.equals(CalendarViewAdapter.loadDate())) {
weeks[rowIndex].days[i].refreshContent(date , State.SELECT);
if(weeks[rowIndex] == null) {
weeks[rowIndex] = new Week(rowIndex);
}
if(weeks[rowIndex].days[i] != null) {
if (date.equals(CalendarViewAdapter.loadDate())) {
weeks[rowIndex].days[i].setState(State.SELECT);
weeks[rowIndex].days[i].setDate(date);
} else {
weeks[rowIndex].days[i].setState(State.CURRENT_MONTH);
weeks[rowIndex].days[i].setDate(date);
}
} else {
weeks[rowIndex].days[i].refreshContent(date , State.CURRENT_MONTH);
if (date.equals(CalendarViewAdapter.loadDate())) {
weeks[rowIndex].days[i] = new Day(State.SELECT , date , rowIndex , i);
} else {
weeks[rowIndex].days[i] = new Day(State.CURRENT_MONTH , date , rowIndex , i);
}
}
day -- ;
}
Expand Down Expand Up @@ -116,11 +138,22 @@ private int fillWeek(int lastMonthDays, int currentMonthDays, int firstDayWeek,

private void fillCurrentMonthDate(int day, int row, int col) {
CalendarDate date = seedDate.modifyDay(day);
if(weeks[row] == null) {
weeks[row] = new Week(row);
}
if(weeks[row].days[col] != null) {
if(date.equals(CalendarViewAdapter.loadDate())) {
weeks[row].days[col].refreshContent(seedDate.modifyDay(day) , State.SELECT);
weeks[row].days[col].setDate(date);
weeks[row].days[col].setState(State.SELECT);
} else {
weeks[row].days[col].refreshContent(seedDate.modifyDay(day) , State.CURRENT_MONTH);
weeks[row].days[col].setDate(date);
weeks[row].days[col].setState(State.CURRENT_MONTH);
}
} else {
if(date.equals(CalendarViewAdapter.loadDate())) {
weeks[row].days[col] = new Day(State.SELECT , date , row , col);
} else {
weeks[row].days[col] = new Day(State.CURRENT_MONTH , date , row , col);
}
}
if(date.equals(seedDate)){
Expand All @@ -133,8 +166,14 @@ private void instantiateNextMonth(int currentMonthDays, int firstDayWeek, int ro
seedDate.year,
seedDate.month + 1,
position - firstDayWeek - currentMonthDays + 1);
if(weeks[row] == null) {
weeks[row] = new Week(row);
}
if(weeks[row].days[col] != null) {
weeks[row].days[col].refreshContent(date, State.NEXT_MONTH);
weeks[row].days[col].setDate(date);
weeks[row].days[col].setState(State.NEXT_MONTH);
} else {
weeks[row].days[col] = new Day(State.NEXT_MONTH , date , row , col);
}
// TODO: 17/6/27 当下一个月的天数大于七时,说明该月有六周
// if(position - firstDayWeek - currentMonthDays + 1 >= 7) { //当下一个月的天数大于七时,说明该月有六周
Expand All @@ -146,8 +185,14 @@ private void instantiateLastMonth(int lastMonthDays, int firstDayWeek, int row,
seedDate.year,
seedDate.month - 1,
lastMonthDays - (firstDayWeek- position - 1));
if(weeks[row] == null) {
weeks[row] = new Week(row);
}
if(weeks[row].days[col] != null) {
weeks[row].days[col].refreshContent(date , State.NEXT_MONTH);
weeks[row].days[col].setDate(date);
weeks[row].days[col].setState(State.PAST_MONTH);
} else {
weeks[row].days[col] = new Day(State.PAST_MONTH , date , row , col);
}
}

Expand All @@ -174,7 +219,7 @@ public void cancelSelectState(){
if (weeks[i] != null){
for (int j = 0; j < Const.TOTAL_COL; j++){
if(weeks[i].days[j].getState() == State.SELECT){
weeks[i].days[j].refreshState(State.CURRENT_MONTH);
weeks[i].days[j].setState(State.CURRENT_MONTH);
resetSelectedRowIndex();
break;
}
Expand Down Expand Up @@ -223,11 +268,7 @@ public void setOnSelectDateListener(OnSelectDateListener onSelectDateListener) {
this.onSelectDateListener = onSelectDateListener;
}

public void setWeeks(Week[] weeks) {
this.weeks = weeks;
}

public Week[] getWeeks() {
return weeks;
public void setDayRenderer(IDayRenderer dayRenderer) {
this.dayRenderer = dayRenderer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
import android.view.View;
import android.view.ViewGroup;

import com.ldf.calendar.Const;
import com.ldf.calendar.interf.OnAdapterSelectListener;
import com.ldf.calendar.interf.IDayRenderer;
import com.ldf.calendar.interf.OnSelectDateListener;
import com.ldf.calendar.Utils;
import com.ldf.calendar.view.MonthPager;
import com.ldf.calendar.model.CalendarDate;
import com.ldf.calendar.view.Calendar;
import com.ldf.calendar.view.Week;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -35,7 +33,7 @@ public CalendarViewAdapter(Context context ,
super();
this.calendarType = calendarType;
init(context, onSelectDateListener);
setCustomDayView(dayView);
setCustomDayRenderer(dayView);
}

private void init(Context context, OnSelectDateListener onSelectDateListener) {
Expand Down Expand Up @@ -87,8 +85,6 @@ public Object instantiateItem(ViewGroup container, int position) {
}//每周的种子日期为这一周的最后一天
calendar.updateWeek(rowCount);
}
calendar.getCellHeight();

container.addView(calendar, 0);
return calendar;
}
Expand Down Expand Up @@ -251,45 +247,14 @@ public CalendarAttr.CalendayType getCalendarType() {
return calendarType;
}

public void setCustomDayView(IDayRenderer dayRenderer) {
Week weeks0[] = new Week[Const.TOTAL_ROW];
for (int j = 0 ; j < 6 ; j ++) {
Week week = new Week(j);
IDayRenderer[] days = new IDayRenderer[Const.TOTAL_COL];
for (int i = 0 ; i < 7 ; i ++) {
days[i] = dayRenderer.copy();
}
week.setDays(days);
weeks0[j] = week;
}
public void setCustomDayRenderer(IDayRenderer dayRenderer) {
Calendar c0 = calendars.get(0);
c0.setWeeks(weeks0);

Week weeks1[] = new Week[Const.TOTAL_ROW];
for (int j = 0 ; j < 6 ; j ++) {
Week week = new Week(j);
IDayRenderer[] days = new IDayRenderer[Const.TOTAL_COL];
for (int i = 0 ; i < 7 ; i ++) {
days[i] = dayRenderer.copy();
}
week.setDays(days);
weeks1[j] = week;
}
c0.setDayRenderer(dayRenderer);

Calendar c1 = calendars.get(1);
c1.setWeeks(weeks1);

Week weeks2[] = new Week[Const.TOTAL_ROW];
for (int j = 0 ; j < 6 ; j ++) {
Week week = new Week(j);
IDayRenderer[] days = new IDayRenderer[Const.TOTAL_COL];
for (int i = 0 ; i < 7 ; i ++) {
days[i] = dayRenderer.copy();
}
week.setDays(days);
weeks2[j] = week;
}
c1.setDayRenderer(dayRenderer.copy());

Calendar c2 = calendars.get(2);
c2.setWeeks(weeks2);
c2.setDayRenderer(dayRenderer.copy());
}
}
13 changes: 3 additions & 10 deletions calendar/src/main/java/com/ldf/calendar/interf/IDayRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.ldf.calendar.component.State;
import com.ldf.calendar.model.CalendarDate;
import com.ldf.calendar.view.Day;
import com.ldf.calendar.view.DayView;

/**
Expand All @@ -12,18 +13,10 @@

public interface IDayRenderer {

void refreshDate(CalendarDate date);
void refreshContent();

void refreshState(State state);

void refreshContent(CalendarDate date , State state);

State getState();

CalendarDate getDate();
void drawDay(Canvas canvas, Day day);

IDayRenderer copy();

void drawDay(Canvas canvas, float posX, float posY);

}
5 changes: 3 additions & 2 deletions calendar/src/main/java/com/ldf/calendar/view/Calendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.view.View;

import com.ldf.calendar.Const;
import com.ldf.calendar.interf.IDayRenderer;
import com.ldf.calendar.interf.OnAdapterSelectListener;
import com.ldf.calendar.component.CalendarAttr;
import com.ldf.calendar.component.CalendarRenderer;
Expand Down Expand Up @@ -143,7 +144,7 @@ public CalendarDate getSeedDate() {
return renderer.getSeedDate();
}

public void setWeeks(Week[] weeks){
renderer.setWeeks(weeks);
public void setDayRenderer(IDayRenderer dayRenderer){
renderer.setDayRenderer(dayRenderer);
}
}
Loading

0 comments on commit 062ac4e

Please sign in to comment.