java自定义日历控件_使用Calendar实现自定义日历

class DatePickerDialog2 : BaseActivity() {

private var timeMillis = 0

private var mViews = ArrayList()

private var pAdapter: MyAdapter? = null

private var isStart = 0

private var isEnd = 0

companion object {

const val REQUEST_CODE = 1718

fun show(context: Activity) {

val intent = Intent(context, DatePickerDialog2::class.java)

context.startActivityForResult(intent, REQUEST_CODE)

}

}

override fun bindlayout(): Int = R.layout.fragment_date_picker2

override fun initListener() {

tvPre.setOnClickListener {

if (mPager.currentItem > 0) {

mPager.setCurrentItem(mPager.currentItem - 1, true)

}

}

tvNext.setOnClickListener {

mPager.setCurrentItem(mPager.currentItem + 1, true)

}

tvCancel.setOnClickListener { finish() }

tvConfirm.setOnClickListener {

if (isStart == 0) {

toast("请选择开始日期")

return@setOnClickListener

}

if (isEnd == 0) {

toast("请选择结束日期")

return@setOnClickListener

}

val intent = Intent()

intent.putExtra("start", isStart)

intent.putExtra("end", isEnd)

setResult(Activity.RESULT_OK, intent)

finish()

}

}

override fun init() {

timeMillis = (Calendar.getInstance().timeInMillis / 1000).toInt()

tvDate.bindText(TimeUtils.formatDate("yyyy年MM月", timeMillis.toString()))

initAdapter()

mPager.addOnPageChangeListener(object :ViewPager.SimpleOnPageChangeListener() {

override fun onPageSelected(position: Int) {

tvDate.bindText(TimeUtils.formatDate("yyyy年MM月",mViews[position].timeMillis.toString()))

if (position == mViews.size - 1) {

val a = Calendar.getInstance()

a.timeInMillis = timeMillis.toLong() * 1000

a.add(Calendar.MONTH, 1)

timeMillis = (a.timeInMillis / 1000).toInt()

initAdapter()

}

}

})

}

private fun initAdapter() {

val mDatas = ArrayList()

for (i in 1 until getWeekOfFirstDayInMonth()) {

mDatas.add(DatePickerBean2(0, 0))

}

val b = Calendar.getInstance()

b.timeInMillis = timeMillis.toLong() * 1000

b.set(Calendar.DATE, 1)

for (i in 0 until getDaysOfMonth(timeMillis)) {

mDatas.add(DatePickerBean2((b.timeInMillis / 1000).toInt(), 0))

b.add(Calendar.DATE, 1)

}

val aadapter = DatePickerAdapter2(this@DatePickerDialog2, mDatas)

val contents = RecyclerView(this@DatePickerDialog2)

contents.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)

contents.apply {

layoutManager = GridLayoutManager(this@DatePickerDialog2, 7)

setHasFixedSize(true)

adapter = aadapter

}

aadapter.setOnItemClickListener {

when (mDatas[it].selectType) {

0 ->

{

if (isStart == 0) {

if (isEnd == 0 || mDatas[it].timeMillis < isEnd) {

mDatas[it].selectType = 1

isStart = mDatas[it].timeMillis

} else {

toast("开始日期不能在结束日期之后哦")

}

} else if (isEnd == 0) {

if (isStart == 0 || isStart < mDatas[it].timeMillis) {

mDatas[it].selectType = 2

isEnd = mDatas[it].timeMillis

} else {

toast("结束日期不能在开始日期之前哦")

}

}

}

1 -> {

mDatas[it].selectType = 0

isStart = 0

}

2 -> {

mDatas[it].selectType = 0

isEnd = 0

}

}

aadapter.refresh(mDatas).notifyItemChanged(it)

}

if (pAdapter == null) {

pAdapter = MyAdapter()

mViews.add(MyView(contents, timeMillis))

mPager.adapter = pAdapter

if (mPager.currentItem == mViews.size - 1) {

val a = Calendar.getInstance()

a.timeInMillis = timeMillis.toLong() * 1000

a.add(Calendar.MONTH, 1)

timeMillis = (a.timeInMillis / 1000).toInt()

initAdapter()

}

} else {

mViews.add(MyView(contents, timeMillis))

mPager.adapter.notifyDataSetChanged()

}

}

private inner class MyAdapter : PagerAdapter() {

override fun isViewFromObject(view: View?, obj: Any?): Boolean = view == obj

override fun getCount(): Int = mViews.size

override fun instantiateItem(container: ViewGroup, position: Int): Any {

container.addView(mViews[position].view)

return mViews[position].view

}

override fun destroyItem(container: ViewGroup, position: Int, `object`: Any?) {

container.removeView(mViews[position].view)

}

}

private fun getWeekOfFirstDayInMonth(): Int {

val a = Calendar.getInstance()

a.timeInMillis = timeMillis.toLong() * 1000

a.set(Calendar.DATE, 1)

return a.get(Calendar.DAY_OF_WEEK)

}

data class MyView(val view: View, val timeMillis: Int)

private fun getDaysOfMonth(timeMillis: Int): Int {

val a = Calendar.getInstance()

a.timeInMillis = timeMillis.toLong() * 1000

return a.getActualMaximum(Calendar.DATE)

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值