Skip to content

Commit a11091d

Browse files
committed
fix mode switch
1 parent c2b14c2 commit a11091d

File tree

8 files changed

+48
-37
lines changed

8 files changed

+48
-37
lines changed

.idea/misc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+5-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<img src="./resources/screen-1.gif" width="30%" height="30%">
44

55
#CalendarView
6-
As the CalendarView projects in GitHub seems to be not so much **customizable**,this CalendarView was born and here is what i achieve.
6+
As the CalendarView projects in GitHub seem to be not so much **customizable**,this CalendarView was born and here is what i achieve.
77
- basically,`WeekView` (the title) background color customizable, `DayView` text background color customizable
88
- what exactly important is, DayView **click** customizable by implementing [DayClickListener](https://github.com/CallMeXYZ/CalendarView/blob/master/library/src/main/java/com/callmexyz/calendarview/dayclicklistener/DayClickListener.java) and calling `setDayClickListener`.
99

@@ -16,7 +16,7 @@ As the CalendarView projects in GitHub seems to be not so much **customizable**,
1616

1717
# logs
1818
## 2015.4.5
19-
- fix Utils.getWeekDiff. Why Java Calendar seems so powerless when it comes to calculate difference!Maybe u can take a coffee time to look at [joda-time-android](https://github.com/dlew/joda-time-android) if u don't want to be f***ed up.LOl
19+
- fix Utils.getWeekDiff. Why Java Calendar seems so powerless when it comes to calculate difference!Maybe u can take a coffee time to look at [joda-time-android](https://github.com/dlew/joda-time-android) if u don't want to be f***ed up.Lol
2020

2121
## 2015.4.1
2222
- happy Fool's Day!Now the CalendarView can make u money per-millis! if u believe me...
@@ -33,7 +33,9 @@ As the CalendarView projects in GitHub seems to be not so much **customizable**,
3333

3434
# Issues
3535
## 2016.4.5
36-
- switch to week mode,sometimes it cannot navigate to the selected Calendar and unclick seems to be blocked until slide out the 3 limit page of PagerAdapter;
36+
- ~~switch to week mode,sometimes it cannot navigate to the selected Calendar and unclick seems to be blocked until slide out the 3 limit page of PagerAdapter;~~ *2016.4.6*
37+
~~override the `getItemPosition` method and return `POSITION_NONE` to force refresh in `MonthPagerAdapter`~~
38+
recreate `mMonthPagerAdapter` to avoid navigating slide during changing Calendar week mode
3739

3840
## 2016.4.1
3941
- ~~week mode crash?what f!~~ *2016.4.5* deprecate Utils.ifSameWeek,return fake result in CalendarView. Calendar.setFirstDayOfWeek() seems weird
@@ -46,14 +48,8 @@ As the CalendarView projects in GitHub seems to be not so much **customizable**,
4648

4749
## 2016.3.24
4850
- ~~when click,size get smaller~~ *2016.3.31*
49-
5051
~~because `setBackGround(null)`,may be when set background drawable slide will cause resize~~
5152
true reason is CalenderView's padding.I used to set the height of TitleView and MonthItem is times of & and the surplus divided to CalenderView's left padding and right padding.Now remove this idea.
5253

5354
- ~~store day select state~~ *2016.3.24*
54-
5555
the cache page is only 3 ,so the stored selected DayView in CalendarView is most likely not the same instance after slide back!!!
56-
57-
58-
59-

app/src/main/java/com/xyz/calendarview/TestActivity.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.support.annotation.ColorInt;
66
import android.view.View;
77
import android.widget.TextView;
8+
import android.widget.Toast;
89

910
import com.callmexyz.calendarview.CalendarView;
1011
import com.callmexyz.calendarview.DayView;
@@ -54,10 +55,9 @@ public void onClick(View v) {
5455
});
5556

5657

57-
/*
58-
example for selectDay using selectDay(Calendar) when initiating
59-
or u can just call selectDayAtInit(Calendar);
60-
*//* Thread t = new Thread(){
58+
// example for selectDay using selectDay(Calendar) when initiating
59+
// or u can just call selectDayAtInit(Calendar);
60+
/* Thread t = new Thread(){
6161
@Override
6262
public void run() {
6363
super.run();
@@ -88,15 +88,14 @@ public void run() {
8888
findViewById(R.id.test).setOnClickListener(new View.OnClickListener() {
8989
@Override
9090
public void onClick(View v) {
91-
// calendarView.startCollapse();
9291
MonthViewStyle w = calendarView.getMonthViewStyle();
9392
if (w.getMonthType() == MonthViewStyle.MonthType.MONTH_VIEW)
9493
w.setMonthType(MonthViewStyle.MonthType.WEEK_VIEW);
9594
else w.setMonthType(MonthViewStyle.MonthType.MONTH_VIEW);
9695
calendarView.setMonthViewStyle(w);
9796
}
98-
9997
});
98+
10099
}
101100

102101
class MyDayClickListener extends CircleColor {
@@ -108,14 +107,15 @@ public MyDayClickListener(@ColorInt int color) {
108107
public void onDayClick(DayView view, Calendar c, boolean ifRestoring) {
109108
super.onDayClick(view, c, ifRestoring);
110109
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
111-
if (!ifRestoring) {
112-
tv.setText(sdf.format(new Date(calendarView.getSelectedCalendar().getTimeInMillis())));
113-
}
110+
String s = sdf.format(new Date(calendarView.getSelectedCalendar().getTimeInMillis()));
111+
tv.setText(s);
112+
if (!ifRestoring) Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
114113
}
115114

116115
@Override
117116
public void onDayUnClick(DayView view, Calendar c) {
118117
super.onDayUnClick(view, c);
119118
}
120119
}
120+
121121
}

app/src/main/res/layout/activity_test.xml

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
43
android:layout_width="match_parent"
54
android:layout_height="match_parent"
65
android:orientation="vertical">
@@ -43,9 +42,12 @@
4342

4443
/>
4544

46-
<View
45+
46+
<TextView
4747
android:id="@+id/test"
48-
android:layout_width="match_parent"
49-
android:layout_height="100dp"
50-
android:background="@android:color/holo_blue_light"/>
48+
android:layout_gravity="center"
49+
android:layout_width="wrap_content"
50+
android:layout_height="50dp"
51+
android:gravity="center"
52+
android:text="change mode"/>
5153
</LinearLayout>

library/src/main/java/com/callmexyz/calendarview/CalendarView.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ private void init(Context context, AttributeSet attrs) {
6565
initViews();
6666
}
6767

68-
// TODO: 2016/3/24
6968
private void initStyles(Context context, AttributeSet attr) {
7069
mDayViewStyle = new DayViewStyle();
7170
TypedArray ta = getContext().obtainStyledAttributes(attr, R.styleable.xyz_calendarview, 0, 0);
@@ -338,12 +337,18 @@ public MonthViewStyle getMonthViewStyle() {
338337
// TODO: 2016/4/5 animation
339338
public void setMonthViewStyle(MonthViewStyle mMonthViewStyle) {
340339
this.mMonthViewStyle = mMonthViewStyle;
341-
refreshUI();
342-
if (null != mSelectedCalendar) {
343-
navToPage(mSelectedCalendar);
344-
return;
345-
}
346-
if (null != mCurrentMonthStart) navToPage(mCurrentMonthStart);
340+
mMonthPagerAdapter = new MonthPagerAdapter(this);
341+
mMonthViewPager.setAdapter(mMonthPagerAdapter);
342+
if (null == mSelectedCalendar)
343+
mCurrentPosition = mMonthPagerAdapter.getPosition(Calendar.getInstance());
344+
else mCurrentPosition = mMonthPagerAdapter.getPosition(mSelectedCalendar);
345+
mMonthViewPager.setCurrentItem(mCurrentPosition);
346+
// if (null != mSelectedCalendar) {
347+
// mMonthViewPager.setCurrentItem(mMonthPagerAdapter.getPosition(mSelectedCalendar));
348+
// return;
349+
// }
350+
// if (null != mCurrentMonthStart) mMonthViewPager.setCurrentItem(mMonthPagerAdapter.getPosition(mCurrentMonthStart));
351+
//
347352
}
348353

349354
public Calendar getSelectedCalendar() {
@@ -366,11 +371,13 @@ public MonthPagerAdapter getMonthPagerAdapter() {
366371
return mMonthPagerAdapter;
367372
}
368373

374+
// TODO: 2016/4/6
369375
public void startCollapse() {
370376
collapsing = true;
371377
MyAnimation animation = new MyAnimation();
372378
animation.setFillAfter(true);
373379
startAnimation(animation);
380+
374381
}
375382

376383
/**
@@ -458,4 +465,6 @@ protected void applyTransformation(float interpolatedTime, Transformation t) {
458465
requestLayout();
459466
}
460467
}
468+
469+
461470
}

library/src/main/java/com/callmexyz/calendarview/MonthPagerAdapter.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void setRange(Calendar start, Calendar end) {
4646

4747
public void refreshUI() {
4848
mViewList.clear();
49-
notifyDataSetChanged();
49+
notifyDataSetChanged();
5050
}
5151

5252
public boolean checkCalendar(Calendar c) {
@@ -67,7 +67,7 @@ private int getCount(Calendar start, Calendar end) {
6767
}
6868
if (MonthViewStyle.MonthType.MONTH_VIEW == mCalendarView.getMonthViewStyle().getMonthType())
6969
return Utils.getMonthDiff(start, end) + 1;
70-
return Utils.getWeekDiff(start, end,mCalendarView.getFirstDayOfWeek()) + 1;
70+
return Utils.getWeekDiff(start, end, mCalendarView.getFirstDayOfWeek()) + 1;
7171
}
7272

7373
@Override
@@ -91,6 +91,11 @@ public void destroyItem(ViewGroup container, int position, Object object) {
9191

9292
}
9393

94+
/* @Override
95+
public int getItemPosition(Object object) {
96+
return POSITION_NONE;
97+
}*/
98+
9499
public Calendar getItemCalendar(int position) {
95100
Calendar c = (Calendar) mRangeStart.clone();
96101
if (MonthViewStyle.MonthType.MONTH_VIEW == mCalendarView.getMonthViewStyle().getMonthType())
@@ -103,11 +108,11 @@ public int getPosition(Calendar c) {
103108
int i = 0;
104109
if (MonthViewStyle.MonthType.MONTH_VIEW == mCalendarView.getMonthViewStyle().getMonthType())
105110
i = Utils.getMonthDiff(mRangeStart, c);
106-
else i = Utils.getWeekDiff(mRangeStart, c,mCalendarView.getFirstDayOfWeek());
111+
else i = Utils.getWeekDiff(mRangeStart, c, mCalendarView.getFirstDayOfWeek());
107112
if (i < 0 || i > getCount() - 1) {
108113
Log.w(TAG, "the given calendar is out of valid range");
109114
}
110-
return i < 0 ? 0 : (i > getCount()-1 ? getCount()-1 : i);
115+
return i < 0 ? 0 : (i > getCount() - 1 ? getCount() - 1 : i);
111116
}
112117

113118
/**
@@ -116,7 +121,7 @@ public int getPosition(Calendar c) {
116121
*/
117122
public DayView getDayView(Calendar c) {
118123
for (int i = 0; i < mViewList.size(); i++) {
119-
if ((MonthViewStyle.MonthType.MONTH_VIEW == mCalendarView.getMonthViewStyle().getMonthType()
124+
if ((MonthViewStyle.MonthType.MONTH_VIEW == mCalendarView.getMonthViewStyle().getMonthType()
120125
&& Utils.ifSameMonth(mViewList.get(i).getFirstDay(), c))
121126
|| (MonthViewStyle.MonthType.WEEK_VIEW == mCalendarView.getMonthViewStyle().getMonthType()
122127
&& Utils.ifSameWeekAsWeekStart(mViewList.get(i).getMonthStartDay(), c))) {

library/src/main/java/com/callmexyz/calendarview/Utils.java

-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ public static boolean ifSameWeekAsWeekStart(Calendar weekStart, Calendar cal) {
7878
if (weekStart.after(cal)) return false;
7979
return getDayDifference(weekStart, cal) < 7;
8080
}
81-
// TODO: 2016/4/5
8281

8382
/**
8483
* @param cc1

resources/screen-1.gif

798 KB
Loading

0 commit comments

Comments
 (0)