android實現(xiàn)切換日期左右無限滑動效果
大家在進(jìn)行安卓開發(fā)的時候,經(jīng)常用到日期比如在課程安排,工作安排,日志等地方,今天給大家?guī)淼氖亲屓掌谧笥覠o限的滑動,并支持自定義顯示效果的方法。一起來學(xué)習(xí)下。

以上是本次所要達(dá)到的效果
使用說明:
XML布局
新建XML布局
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>
自定義日歷樣式
新建CustomDayView繼承自DayView并重寫refreshContent 和 copy 兩個方法
@Override
public void refreshContent() {
//你的代碼 你可以在這里定義你的顯示規(guī)則
super.refreshContent();
}
@Override
public IDayRenderer copy() {
return new CustomDayView(context , layoutResource);
}
新建CustomDayView實例,并作為參數(shù)構(gòu)建CalendarViewAdapter
CustomDayView customDayView = new CustomDayView(
context , R.layout.custom_day);
calendarAdapter = new CalendarViewAdapter(
context ,
onSelectDateListener ,
Calendar.MONTH_TYPE ,
customDayView);
初始化View
目前來看 相比于Dialog選擇日歷 我的控件更適合于Activity/Fragment在Activity的onCreate 或者Fragment的onCreateView 你需要實現(xiàn)這兩個方法來啟動日歷并裝填進(jìn)數(shù)據(jù)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_syllabus);
initCalendarView();
}
private void initCalendarView() {
initListener();
CustomDayView customDayView = new CustomDayView(
context , R.layout.custom_day);
calendarAdapter = new CalendarViewAdapter(
context ,
onSelectDateListener ,
Calendar.MONTH_TYPE ,
customDayView);
initMarkData();
initMonthPager();
}
使用此方法回調(diào)日歷點擊事件
private void initListener() {
onSelectDateListener = new OnSelectDateListener() {
@Override
public void onSelectDate(CalendarDate date) {
//your code
}
@Override
public void onSelectOtherMonth(int offset) {
//偏移量 -1表示上一個月 , 1表示下一個月
monthPager.selectOtherMonth(offset);
}
};
}
使用此方法初始化日歷標(biāo)記數(shù)據(jù)
private void initMarkData() {
HashMap markData = new HashMap<>();
//1表示紅點,0表示灰點
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)聽
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) {
}
});
重寫onWindowFocusChanged方法,使用此方法得知calendar和day的尺寸
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus && !initiated) {
CalendarDate today = new CalendarDate();
calendarAdapter.notifyDataChanged(today);
initiated = true;
}
}
Download
Gradle: Step 1. Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://www.jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
compile 'com.github.MagicMashRoom:SuperCalendar:v1.3.1'
}
以上就是本次效果所用到的所有代碼和說明,大家有任何問題可以在下方的留言地方討論。
- Android實現(xiàn)界面左右滑動切換功能
- Android App中使用ViewPager+Fragment實現(xiàn)滑動切換效果
- Android開發(fā)之使用ViewPager實現(xiàn)圖片左右滑動切換效果
- Android實現(xiàn)簡單底部導(dǎo)航欄 Android仿微信滑動切換效果
- Android實現(xiàn)滑動屏幕切換圖片
- Android編程實現(xiàn)ViewPager多頁面滑動切換及動畫效果的方法
- Android實現(xiàn)微信首頁左右滑動切換效果
- Android使用TabLayou+fragment+viewpager實現(xiàn)滑動切換頁面效果
- Android Listview上下拉動刷新tab滑動切換功能
- android?viewflipper實現(xiàn)左右滑動切換顯示圖片
相關(guān)文章
Android實現(xiàn)旋轉(zhuǎn)動畫的兩種方式案例詳解
這篇文章主要介紹了Android實現(xiàn)旋轉(zhuǎn)動畫的兩種方式,需要的朋友可以參考下2021-08-08
Android Bitmap的截取及狀態(tài)欄的隱藏和顯示功能
Bitmap是Android系統(tǒng)中的圖像處理中最重要類之一。Bitmap可以獲取圖像文件信息,對圖像進(jìn)行剪切、旋轉(zhuǎn)、縮放,壓縮等操作,并可以以指定格式保存圖像文件。這篇文章主要介紹了Android Bitmap的截取及狀態(tài)欄的隱藏和顯示功能,需要的朋友可以參考下2017-11-11
使用AndroidStudio上傳忽略文件至SVN Server的解決辦法
這篇文章主要介紹了使用AndroidStudio上傳忽略文件至SVN Server的解決辦法 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-06-06
RecyclerView+SnapHelper實現(xiàn)無限循環(huán)篩選控件
這篇文章主要為大家詳細(xì)介紹了RecyclerView+SnapHelper實現(xiàn)無限循環(huán)篩選控件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10
eclipse中運行monkeyrunner腳本之環(huán)境搭建(4)
這篇文章主要為大家詳細(xì)介紹了eclipse中運行monkeyrunner腳本之環(huán)境搭建的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
Android開發(fā)實現(xiàn)讀取excel數(shù)據(jù)并保存為xml的方法
這篇文章主要介紹了Android開發(fā)實現(xiàn)讀取excel數(shù)據(jù)并保存為xml的方法,涉及Android針對Excel數(shù)據(jù)讀取及xml格式文件的構(gòu)造與保存相關(guān)操作技巧,需要的朋友可以參考下2017-10-10

