原文地址:https://github.com/MagicMashRoom/SuperCalendar
簡(jiǎn)介 由于項(xiàng)目的需求,研究了眾多日歷軟件。本軟件是一款高仿小米的自定義日歷 > 控件珊燎,周月視圖平滑滾動(dòng),平滑切換陈哑,可以在 xml 文件中進(jìn)行屬性的配置定制搜吧,加入你自己的 RecyclerView 后,實(shí)現(xiàn)日歷與列表的無(wú)縫連接坦胶。
compile 'com.github.MagicMashRoom:SuperCalendar:v1.1.0'
Include MonthPager in your layout XML. MonthPager必須和下方的RecyclerView包裹在同一個(gè)CoordinatorLayout內(nèi).且RecyclerView的layout_behavior為com.ldf.calendar.behavior.RecyclerViewBehavior
<android.support.design.widget.CoordinatorLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<com.ldf.calendar.view.MonthPager
android:id="@+id/calendar_view"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#fff">
</com.ldf.calendar.view.MonthPager>
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="com.ldf.calendar.behavior.RecyclerViewBehavior"
android:background="#c2c2c2"
android:layout_gravity="bottom"/>
</android.support.design.widget.CoordinatorLayout>
- 目前來(lái)看 相比于Dialog選擇日歷 我的控件更適合于Activity/Fragment在Activity的onCreate 或者Fragment的onCreateView 你需要實(shí)現(xiàn)這兩個(gè)方法來(lái)啟動(dòng)日歷并裝填進(jìn)數(shù)據(jù)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_syllabus);
initCalendarView();
}
private void initCalendarView() {
initListener();
calendarAdapter = new CalendarViewAdapter(context ,
onSelectDateListener ,
Calendar.MONTH_TYPE);
initMarkData();
initMonthPager();
}
- 使用此方法回調(diào)日歷點(diǎn)擊事件
private void initListener() {
onSelectDateListener = new OnSelectDateListener() {
@Override
public void onSelectDate(CalendarDate date) {
refreshClickDate(date);
}
@Override
public void onSelectOtherMonth(int offset) {
monthPager.setCurrentItem(mCurrentPage + offset);
}
};
}
- 使用此方法初始化日歷標(biāo)記數(shù)據(jù)
private void initMarkData() {
HashMap markData = new HashMap<>();
markData.put("2017-8-9" , "1");
markData.put("2017-7-9" , "0");
markData.put("2017-6-9" , "1");
markData.put("2017-6-10" , "0");
calendarAdapter.setMarkData(markData);
}
使用此方法給MonthPager添加上相關(guān)監(jiān)聽(tīng)
monthPager.addOnPageChangeListener(new MonthPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mCurrentPage = position;
currentCalendars = calendarAdapter.getAllItems();
if(currentCalendars.get(position % currentCalendars.size()) instanceof Calendar){
//you code
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});