Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 日曆控件庫:支持左右滑動與農曆節假日顯示

編輯:關於 Android 程式設計

封面圖

演示效果

源碼目錄結構

功能特性

  1. 日曆支持左右滑動切換月份;
  2. 同時顯示陽曆、農曆、節假日與二十四節氣;
  3. 支持對某月日期進行單選或多選。

接入方式

在項目 build.gradle 中加入依賴:

compile 'com.joybar.calendar:librarycalendar:1.0.4'

使用示例

Activity 需要實現三個接口:OnPageChangeListener(月份翻頁)、OnDateClickListener(點選日期),若要多選還需實現 OnDateCancelListener(取消選中)。

public class MainActivity extends AppCompatActivity
        implements CalendarViewPagerFragment.OnPageChangeListener,
                   CalendarViewFragment.OnDateClickListener,
                   CalendarViewFragment.OnDateCancelListener {

    private TextView tv_date;
    private boolean isChoiceModelSingle = false;
    private List<CalendarDate> mListDate = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_date = (TextView) findViewById(R.id.tv_date);
        initFragment();
    }

    private void initFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction tx = fm.beginTransaction();
        Fragment fragment = CalendarViewPagerFragment.newInstance(isChoiceModelSingle);
        tx.replace(R.id.fl_content, fragment);
        tx.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_im, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_single:
                isChoiceModelSingle = true;
                initFragment();
                break;
            case R.id.menu_multi:
                isChoiceModelSingle = false;
                initFragment();
                break;
            default:
                break;
        }
        return true;
    }

    @Override
    public void OnDateClick(CalendarDate calendarDate) {
        int year = calendarDate.getSolar().solarYear;
        int month = calendarDate.getSolar().solarMonth;
        int day = calendarDate.getSolar().solarDay;
        if (isChoiceModelSingle) {
            tv_date.setText(year + "-" + month + "-" + day);
        } else {
            mListDate.add(calendarDate);
            tv_date.setText(listToString(mListDate));
        }
    }

    @Override
    public void OnDateCancel(CalendarDate calendarDate) {
        int count = mListDate.size();
        for (int i = 0; i < count; i++) {
            CalendarDate date = mListDate.get(i);
            if (date.getSolar().solarDay == calendarDate.getSolar().solarDay) {
                mListDate.remove(i);
                break;
            }
        }
        tv_date.setText(listToString(mListDate));
    }

    @Override
    public void OnPageChange(int year, int month) {
        // 翻頁時清空已選,避免跨月選擇造成歧義
        tv_date.setText(year + "-" + month);
        mListDate.clear();
    }

    private static String listToString(List<CalendarDate> list) {
        StringBuffer stringBuffer = new StringBuffer();
        for (CalendarDate date : list) {
            stringBuffer.append(date.getSolar().solarYear + "-"
                    + date.getSolar().solarMonth + "-"
                    + date.getSolar().solarDay).append(" ");
        }
        return stringBuffer.toString();
    }
}

單選與多選的底層實現

控件內部其實是一個 GridView,透過 setChoiceMode() 切換選擇模式,並在 item 點擊時判斷所點格子是否屬於當月:

if (isChoiceModelSingle) {
    mGridView.setChoiceMode(GridView.CHOICE_MODE_SINGLE);
} else {
    mGridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE);
}

mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
        CalendarDate calendarDate = ((CalendarGridViewAdapter) mGridView
                .getAdapter()).getListData().get(position);
        if (isChoiceModelSingle) {
            // 單選:只響應當月格子
            if (finalMListDataCalendar.get(position).isInThisMonth()) {
                onDateClickListener.OnDateClick(calendarDate);
            } else {
                mGridView.setItemChecked(position, false);
            }
        } else {
            // 多選:依當前勾選狀態決定是選還是取消
            if (finalMListDataCalendar.get(position).isInThisMonth()) {
                if (!mGridView.isItemChecked(position)) {
                    onDateCancelListener.OnDateCancel(calendarDate);
                } else {
                    onDateClickListener.OnDateClick(calendarDate);
                }
            } else {
                mGridView.setItemChecked(position, false);
            }
        }
    }
});

這裡有個值得注意的設計:日曆格子通常是「上月補齊 + 本月 + 下月補齊」的結構,因此必須用 isInThisMonth() 過濾掉補位格子,否則使用者會選到看起來不屬於當月的日期。

項目地址:https://github.com/myjoybar/android-calendar-view

整體來說,這個庫把農曆、節氣、節假日這些麻煩的計算封裝得比較徹底,業務層只需要關心 CalendarDate 這一個數據對象。如果你的項目需要日期選擇能力,直接接入比自己造輪子划算得多。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved