Andorid 之日历控件,可左右滑动,包含公历,农历,节假日等

为什么80%的码农都做不了架构师?>>>   hot3.png

公司项目需要日历这个功能,经过查阅资料写了个demo,包含公历,农历,节假日等,还可左右滑动。

效果图:

代码:

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

    private TextView tv_date;
    private List 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();
        tx.replace(R.id.fl_content, fragment);
        tx.commit();
    }

    @Override
    public void onDateClick(CalendarDate calendarDate) {

        int year = calendarDate.getSolar().solarYear;
        int month = calendarDate.getSolar().solarMonth;
        int day = calendarDate.getSolar().solarDay;
        String data;
        if (day < 10) {
            data = DateUtils.getWeekday(year + "-" + month + "-0" + day);
            tv_date.setText(year + "-" + month + "-0" + day + " " + month + "月" + "  " + data);
        } else {
            data = DateUtils.getWeekday(year + "-" + month + "-" + day);
            tv_date.setText(year + "-" + month + "-" + day + " " + month + "月" + "  " + data);
        }

    }

    @Override
    public void onDateCancel(CalendarDate calendarDate) {
    }

    @Override
    public void onPageChange(int year, int month) {
        tv_date.setText(year + "-" + month);
        mListDate.clear();
    }

}

由于代码太多,完整代码未给出,源码直接下载即可

源码点击下载

转载于:https://my.oschina.net/zhangqie/blog/833036

你可能感兴趣的:(Andorid 之日历控件,可左右滑动,包含公历,农历,节假日等)