Andorid的日历控件库可以显示阳历,农历,节假日和二十四节气实

时间:2016-12-23 09:15 来源:互联网 作者:源码搜藏收藏

  • 源码类别:时间Time|Date
  • 源码大小:未知
  • 编码格式:gbk,utf8,不限
  • 运行环境:Android studio
  • 广告推荐

Android的日历视图

简单易用,功能强大,易于扩展了Android日历视图的库。
使用方便,易扩展的Andorid的日历控件库

图片

特征

  • 日历左右滑动。
  • 显示阳历,农历,节假日和二十四节气
  • 实现对某月日期的单选或者多选。

使用步骤

依赖摇篮

库添加到项目的build.gradle


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

用法示例

实现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 = new CalendarViewPagerFragment();
       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 {
            //System.out.println(calendarDate.getSolar().solarDay);
            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();
    }

}

单选或者多选的实现代码

       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()) {
                       // mGridView.getCheckedItemIds()
                        if(!mGridView.isItemChecked(position)){
                            onDateCancelListener.onDateCancel(calendarDate);
                        } else {
                            onDateClickListener.onDateClick(calendarDate);
                        }

                    } else {
                        mGridView.setItemChecked(position, false);
                    }

                }
            }
        });

本站资源仅限于学习研究,严禁从事商业或者非法活动! 源码搜藏网所有源码来自互联网转载与用户上传分享,如果侵犯了您的权益请与我们联系,我们将在24小时内删除!谢谢!

时间Time|Date下载排行

最新文章