Skip to content

Commit

Permalink
统一事件通知,demo优化
Browse files Browse the repository at this point in the history
  • Loading branch information
huanghaibin-dev committed Dec 4, 2017
1 parent e88bb95 commit acbe66a
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 62 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ Android上一个优雅、高度自定义、性能高效的日历控件,完美

### Gradle
```
compile 'com.haibin:calendarview:3.0.3'
compile 'com.haibin:calendarview:3.0.4'
```
```
<dependency>
<groupId>com.haibin</groupId>
<artifactId>calendarview</artifactId>
<version>3.0.3</version>
<version>3.0.4</version>
<type>pom</type>
</dependency>
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import com.bumptech.glide.RequestManager;
import com.haibin.calendarviewproject.base.adapter.BaseRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
* 适配器
* Created by huanghaibin on 2017/12/4.
Expand All @@ -23,37 +26,8 @@ public class ArticleAdapter extends BaseRecyclerAdapter<Article> {
public ArticleAdapter(Context context) {
super(context);
mLoader = Glide.with(context.getApplicationContext());
mItems.add(create("新西兰克马德克群岛发生5.7级地震 震源深度10千米",
"#地震快讯#中国地震台网正式测定:12月04日08时08分在克马德克群岛(南纬32.82度,西经178.73度)发生5.7级地震,震源深度10千米。",
"http://cms-bucket.nosdn.127.net/catchpic/2/27/27e2ce7fd02e6c096e21b1689a8a3fe9.jpg?imageView&thumbnail=550x0"));
mItems.add(create("俄罗斯喊冤不当\"背锅侠\" 俄美陷入\"后真相\"旋涡",
"“差到令人震惊”,但不怪特朗普。俄罗斯总理德米特里·梅德韦杰夫近日在谈到俄美关系时这样说。俄罗斯近来连遭美国“恶毒”指责和西方国家连环出击。一些国际舆论认为,俄罗斯成了“背锅侠”,俄罗斯自己也公开喊冤斥责美国。在俄美你来我往的互掐中,真相似乎变得已不那么重要了。",
"http://cms-bucket.nosdn.127.net/catchpic/c/c8/c8b0685089258b82f3ca1997def78d8d.png?imageView&thumbnail=550x0"));
mItems.add(create("中企投资巴西获支持 英媒:巴西人感激\"保住饭碗\"",
"参考消息网12月4日报道 英媒称,里约热内卢附近的阿苏港曾被埃克·巴蒂斯塔称为“通往中国的公路”,10多年前,这位现已名誉扫地的巴西前首富创建了这个超级港,大宗商品热潮结束后,他在巴西的商业帝国几乎无一幸存并于2014年破产,但此后至今有一个项目仍蓬勃发展,那就是阿苏港。",
"http://cms-bucket.nosdn.127.net/catchpic/8/8b/8ba2d19b7f63efc5cf714960d5edd2c3.jpg?imageView&thumbnail=550x0"));
mItems.add(create("美电视台记者因误报有关弗林新闻被停职四周",
"【环球网报道】据俄罗斯卫星网12月3日报道,美国ABC电视台记者布莱恩·罗素因在有关美国总统前国家安全顾问迈克尔·弗林的新闻报道中的失误,临时被停职。",
"http://cms-bucket.nosdn.127.net/5d18566fde70407b9cc3a728822115c320171203133214.jpeg?imageView&thumbnail=550x0"));
mItems.add(create("预计明年3月上市 曝全新奥迪Q5L无伪谍照",
"随着之前全新一代国产奥迪Q5L在工信部目录亮相,最近曝光的测试车也都基本褪去了伪装,不过正式上市还是要等到2018年3月份。从最新曝光的内饰来看,轴距的加长令后排的空间有着非常明显的提升。",
"http://cms-bucket.nosdn.127.net/eda9ca222352470190c4f0d6b9a8c29420171201160854.jpeg?imageView&thumbnail=550x0"));
mItems.add(create(
"2019年投产 电咖整车生产基地落户浙江绍兴",
"网易汽车11月30日报道 两周前的广州车展上,电咖发布了其首款电动汽车EV10,官方指导价为13.38万-14.18万,扣除补贴后的零售价为5.98万元-6.78万元,性价比很高。抛开车辆本身,引起业界关注的是这家新势力造车企业的几位核心成员,当年上汽大众团队的三位老兵--张海亮、向东平、牛胜福携手用了957天造了一辆可以上市的车。",
"http://cms-bucket.nosdn.127.net/674c392123254bb69bdd9227442965eb20171129203658.jpeg?imageView&thumbnail=550x0"));
mItems.add(create(
"2017年进入尾声,苹果大笔押注的ARkit还好么?",
"谷歌推出了AR眼镜、ARCore平台和应用在手机上的Project Tango,Facebook也上线了AR开发平台和工具。至于苹果,更是把AR当做发展的重中之重。在新品iPhone8和iPhoneX中,后置摄像头专为AR进行校准,前置摄像头还添加了可以带来更好AR效果的深度传感器。",
"http://cms-bucket.nosdn.127.net/catchpic/7/76/76135ac5d3107a1d5ba11a8ee2fc7e27.jpg?imageView&thumbnail=550x0"));
mItems.add(create(
"亚马逊CTO:我们要让人类成为机器人的中心!",
"那些相信应用下载会让世界变得更美好的智能手机布道者和应用爱好者们,会在AWS re:Invent大会上感到不自在。亚马逊网络服务首席技术官Werner Vogels表示,所有这些都未能实现信息的民主化。",
"http://cms-bucket.nosdn.127.net/ddb758f16a7d4aa3aa422ec385fc3e5020171204081818.jpeg?imageView&thumbnail=550x0"));
mItems.add(create(
"有特斯拉车主想用免费的充电桩挖矿,但这可能行不通",
"在社交网络 Facebook 上的一个特斯拉车主群组中,有人开脑洞说可以尝试自己组装矿机放在特斯拉后备箱中,接入车载电池的电源,然后将车停到超级充电桩附近,就能用免费获得的电力挖矿了。",
"http://crawl.nosdn.127.net/nbotreplaceimg/4ce9c743e6c02f6777d22278e2ef8bc3/2b33e32532db204fe207693c82719660.jpg"));
mItems.addAll(create());
mItems.addAll(create());
}

@Override
Expand Down Expand Up @@ -92,4 +66,40 @@ private static Article create(String title, String content, String imgUrl) {
article.setImgUrl(imgUrl);
return article;
}

private static List<Article> create(){
List<Article> list = new ArrayList<>();
list.add(create("新西兰克马德克群岛发生5.7级地震 震源深度10千米",
"#地震快讯#中国地震台网正式测定:12月04日08时08分在克马德克群岛(南纬32.82度,西经178.73度)发生5.7级地震,震源深度10千米。",
"http://cms-bucket.nosdn.127.net/catchpic/2/27/27e2ce7fd02e6c096e21b1689a8a3fe9.jpg?imageView&thumbnail=550x0"));
list.add(create("俄罗斯喊冤不当\"背锅侠\" 俄美陷入\"后真相\"旋涡",
"“差到令人震惊”,但不怪特朗普。俄罗斯总理德米特里·梅德韦杰夫近日在谈到俄美关系时这样说。俄罗斯近来连遭美国“恶毒”指责和西方国家连环出击。一些国际舆论认为,俄罗斯成了“背锅侠”,俄罗斯自己也公开喊冤斥责美国。在俄美你来我往的互掐中,真相似乎变得已不那么重要了。",
"http://cms-bucket.nosdn.127.net/catchpic/c/c8/c8b0685089258b82f3ca1997def78d8d.png?imageView&thumbnail=550x0"));
list.add(create("中企投资巴西获支持 英媒:巴西人感激\"保住饭碗\"",
"参考消息网12月4日报道 英媒称,里约热内卢附近的阿苏港曾被埃克·巴蒂斯塔称为“通往中国的公路”,10多年前,这位现已名誉扫地的巴西前首富创建了这个超级港,大宗商品热潮结束后,他在巴西的商业帝国几乎无一幸存并于2014年破产,但此后至今有一个项目仍蓬勃发展,那就是阿苏港。",
"http://cms-bucket.nosdn.127.net/catchpic/8/8b/8ba2d19b7f63efc5cf714960d5edd2c3.jpg?imageView&thumbnail=550x0"));
list.add(create("美电视台记者因误报有关弗林新闻被停职四周",
"【环球网报道】据俄罗斯卫星网12月3日报道,美国ABC电视台记者布莱恩·罗素因在有关美国总统前国家安全顾问迈克尔·弗林的新闻报道中的失误,临时被停职。",
"http://cms-bucket.nosdn.127.net/5d18566fde70407b9cc3a728822115c320171203133214.jpeg?imageView&thumbnail=550x0"));
list.add(create("预计明年3月上市 曝全新奥迪Q5L无伪谍照",
"随着之前全新一代国产奥迪Q5L在工信部目录亮相,最近曝光的测试车也都基本褪去了伪装,不过正式上市还是要等到2018年3月份。从最新曝光的内饰来看,轴距的加长令后排的空间有着非常明显的提升。",
"http://cms-bucket.nosdn.127.net/eda9ca222352470190c4f0d6b9a8c29420171201160854.jpeg?imageView&thumbnail=550x0"));
list.add(create(
"2019年投产 电咖整车生产基地落户浙江绍兴",
"网易汽车11月30日报道 两周前的广州车展上,电咖发布了其首款电动汽车EV10,官方指导价为13.38万-14.18万,扣除补贴后的零售价为5.98万元-6.78万元,性价比很高。抛开车辆本身,引起业界关注的是这家新势力造车企业的几位核心成员,当年上汽大众团队的三位老兵--张海亮、向东平、牛胜福携手用了957天造了一辆可以上市的车。",
"http://cms-bucket.nosdn.127.net/674c392123254bb69bdd9227442965eb20171129203658.jpeg?imageView&thumbnail=550x0"));
list.add(create(
"2017年进入尾声,苹果大笔押注的ARkit还好么?",
"谷歌推出了AR眼镜、ARCore平台和应用在手机上的Project Tango,Facebook也上线了AR开发平台和工具。至于苹果,更是把AR当做发展的重中之重。在新品iPhone8和iPhoneX中,后置摄像头专为AR进行校准,前置摄像头还添加了可以带来更好AR效果的深度传感器。",
"http://cms-bucket.nosdn.127.net/catchpic/7/76/76135ac5d3107a1d5ba11a8ee2fc7e27.jpg?imageView&thumbnail=550x0"));
list.add(create(
"亚马逊CTO:我们要让人类成为机器人的中心!",
"那些相信应用下载会让世界变得更美好的智能手机布道者和应用爱好者们,会在AWS re:Invent大会上感到不自在。亚马逊网络服务首席技术官Werner Vogels表示,所有这些都未能实现信息的民主化。",
"http://cms-bucket.nosdn.127.net/ddb758f16a7d4aa3aa422ec385fc3e5020171204081818.jpeg?imageView&thumbnail=550x0"));
list.add(create(
"有特斯拉车主想用免费的充电桩挖矿,但这可能行不通",
"在社交网络 Facebook 上的一个特斯拉车主群组中,有人开脑洞说可以尝试自己组装矿机放在特斯拉后备箱中,接入车载电池的电源,然后将车停到超级充电桩附近,就能用免费获得的电力挖矿了。",
"http://crawl.nosdn.127.net/nbotreplaceimg/4ce9c743e6c02f6777d22278e2ef8bc3/2b33e32532db204fe207693c82719660.jpg"));
return list;
}
}
14 changes: 4 additions & 10 deletions app/src/main/java/com/haibin/calendarviewproject/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

public class MainActivity extends BaseActivity implements
CalendarView.OnDateSelectedListener,
CalendarView.OnDateChangeListener,
CalendarView.OnYearChangeListener,
View.OnClickListener{

TextView mTextMonthDay;
Expand Down Expand Up @@ -66,7 +66,7 @@ public void onClick(View v) {
}
});

mCalendarView.setOnDateChangeListener(this);
mCalendarView.setOnYearChangeListener(this);
mCalendarView.setOnDateSelectedListener(this);
mTextYear.setText(String.valueOf(mCalendarView.getCurYear()));
mYear = mCalendarView.getCurYear();
Expand Down Expand Up @@ -126,8 +126,8 @@ private Calendar getSchemeCalendar(int year, int month, int day, int color, Stri

@SuppressLint("SetTextI18n")
@Override
public void onDateChange(Calendar calendar) {
Log.e("onDateChange"," -- " + calendar.toString());
public void onDateSelected(Calendar calendar) {
Log.e("onDateSelected"," -- " + calendar.toString());
mTextLunar.setVisibility(View.VISIBLE);
mTextYear.setVisibility(View.VISIBLE);
mTextMonthDay.setText(calendar.getMonth() + "月" + calendar.getDay() + "日");
Expand All @@ -136,12 +136,6 @@ public void onDateChange(Calendar calendar) {
mYear = calendar.getYear();
}

@Override
public void onDateSelected(Calendar calendar) {
//onDateChange(calendar);
Log.e("onDateSelected"," -- " + calendar.toString());
}

@Override
public void onYearChange(int year) {
mTextMonthDay.setText(String.valueOf(year));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class SimpleActivity extends BaseActivity implements
CalendarView.OnDateSelectedListener,
CalendarView.OnDateChangeListener,
CalendarView.OnYearChangeListener,
View.OnClickListener {

TextView mTextMonthDay;
Expand Down Expand Up @@ -77,7 +77,7 @@ public void onClick(View v) {
}
});

mCalendarView.setOnDateChangeListener(this);
mCalendarView.setOnYearChangeListener(this);
mCalendarView.setOnDateSelectedListener(this);
mTextYear.setText(String.valueOf(mCalendarView.getCurYear()));
mYear = mCalendarView.getCurYear();
Expand Down Expand Up @@ -142,7 +142,7 @@ private Calendar getSchemeCalendar(int year, int month, int day, int color, Stri

@SuppressLint("SetTextI18n")
@Override
public void onDateChange(Calendar calendar) {
public void onDateSelected(Calendar calendar) {
mTextLunar.setVisibility(View.VISIBLE);
mTextYear.setVisibility(View.VISIBLE);
mTextMonthDay.setText(calendar.getMonth() + "月" + calendar.getDay() + "日");
Expand All @@ -151,11 +151,6 @@ public void onDateChange(Calendar calendar) {
mYear = calendar.getYear();
}

@Override
public void onDateSelected(Calendar calendar) {
onDateChange(calendar);
}


@Override
public void onYearChange(int year) {
Expand Down
4 changes: 2 additions & 2 deletions calendarview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
minSdkVersion 15
//noinspection OldTargetApi
targetSdkVersion 23
versionCode 303
versionName "3.0.3"
versionCode 304
versionName "3.0.4"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* 日历布局
* 各个类使用包权限,避免不必要的public
*/
@SuppressWarnings("deprecation")
public class CalendarView extends FrameLayout {

/**
Expand Down Expand Up @@ -116,12 +117,15 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse

@Override
public void onPageSelected(int position) {
if(mWeekPager.getVisibility() == VISIBLE){
if (mWeekPager.getVisibility() == VISIBLE) {
return;
}
if (mDelegate.mDateChangeListener != null) {
mDelegate.mDateChangeListener.onYearChange(position + mDelegate.getMinYear());
}
if (mDelegate.mYearChangeListener != null) {
mDelegate.mYearChangeListener.onYearChange(position + mDelegate.getMinYear());
}
}

@Override
Expand Down Expand Up @@ -254,6 +258,9 @@ public void scrollToCurrent() {
if (mDelegate.mDateChangeListener != null) {
mDelegate.mDateChangeListener.onDateChange(mDelegate.getCurrentDay());
}
if (mDelegate.mDateSelectedListener != null) {
mDelegate.mDateSelectedListener.onDateSelected(mDelegate.getCurrentDay());
}
}

/**
Expand All @@ -276,14 +283,18 @@ public void closeSelectLayout(final int position) {
mWeekBar.setVisibility(VISIBLE);
mMonthPager.setVisibility(VISIBLE);
if (position == mMonthPager.getCurrentItem()) {
Calendar calendar = new Calendar();
calendar.setYear(position / 12 + mDelegate.getMinYear());
calendar.setMonth(position % 12 + 1);
calendar.setDay(1);
calendar.setLunar(LunarCalendar.numToChineseDay(LunarCalendar.solarToLunar(calendar.getYear(), calendar.getMonth(), 1)[2]));
mDelegate.mSelectedCalendar = calendar;
if (mDelegate.mDateChangeListener != null) {
Calendar calendar = new Calendar();
calendar.setYear(position / 12 + mDelegate.getMinYear());
calendar.setMonth(position % 12 + 1);
calendar.setDay(1);
calendar.setLunar(LunarCalendar.numToChineseDay(LunarCalendar.solarToLunar(calendar.getYear(), calendar.getMonth(), 1)[2]));
mDelegate.mDateChangeListener.onDateChange(calendar);
}
if (mDelegate.mDateSelectedListener != null) {
mDelegate.mDateSelectedListener.onDateSelected(calendar);
}
} else {
mMonthPager.setCurrentItem(position, true);
}
Expand Down Expand Up @@ -321,8 +332,24 @@ public void onAnimationEnd(Animator animation) {
*
* @param listener 监听
*/
@Deprecated
public void setOnDateChangeListener(OnDateChangeListener listener) {
this.mDelegate.mDateChangeListener = listener;
if (mDelegate.mDateChangeListener != null) {
mDelegate.mDateChangeListener.onDateChange(mDelegate.mSelectedCalendar);
}

}


/**
* 年份改变事件
*
* @param listener listener
*/
@SuppressWarnings("unused")
public void setOnYearChangeListener(OnYearChangeListener listener) {
this.mDelegate.mYearChangeListener = listener;
}

/**
Expand All @@ -332,6 +359,9 @@ public void setOnDateChangeListener(OnDateChangeListener listener) {
*/
public void setOnDateSelectedListener(OnDateSelectedListener listener) {
this.mDelegate.mDateSelectedListener = listener;
if (mDelegate.mDateSelectedListener != null) {
mDelegate.mDateSelectedListener.onDateSelected(mDelegate.mSelectedCalendar);
}
}

/**
Expand Down Expand Up @@ -498,12 +528,27 @@ public Calendar getSelectedCalendar() {
}


/**
* 年份改变事件,快速年份切换
*/
public interface OnYearChangeListener {
void onYearChange(int year);
}

/**
* 日期改变、左右切换、快速年份、月份切换
*/
@SuppressWarnings("DeprecatedIsStillUsed")
@Deprecated
public interface OnDateChangeListener {
/**
* 这个方法是准确传递的,但和onDateSelected一样会跟新日历选中状态,造成误区,故新版本建议弃用,
* 统一使用onDateSelected
*/
@Deprecated
void onDateChange(Calendar calendar);

@Deprecated
void onYearChange(int year);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ class CustomCalendarViewDelegate {
*/
CalendarView.OnInnerDateSelectedListener mInnerListener;

/**
* 快速年份切换
*/
CalendarView.OnYearChangeListener mYearChangeListener;

/**
* 保存选中的日期
*/
Expand Down
Loading

0 comments on commit acbe66a

Please sign in to comment.