wrap the month view in a framelayout and add click effects
This commit is contained in:
parent
e3c0883198
commit
7c17438c0c
|
@ -166,7 +166,7 @@ class MonthFragment : Fragment(), MonthlyCalendar {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateDays(days: ArrayList<DayMonthly>) {
|
private fun updateDays(days: ArrayList<DayMonthly>) {
|
||||||
mHolder.month_view.updateDays(days)
|
mHolder.month_view_wrapper.updateDays(days)
|
||||||
|
|
||||||
/*val displayWeekNumbers = mConfig.displayWeekNumbers
|
/*val displayWeekNumbers = mConfig.displayWeekNumbers
|
||||||
val len = days.size
|
val len = days.size
|
||||||
|
|
|
@ -36,13 +36,14 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
|
||||||
weakTextColor = textColor.adjustAlpha(LOW_ALPHA)
|
weakTextColor = textColor.adjustAlpha(LOW_ALPHA)
|
||||||
|
|
||||||
val normalTextSize = resources.getDimensionPixelSize(R.dimen.normal_text_size).toFloat()
|
val normalTextSize = resources.getDimensionPixelSize(R.dimen.normal_text_size).toFloat()
|
||||||
|
weekDaysLetterHeight = 2 * normalTextSize.toInt()
|
||||||
|
|
||||||
paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||||||
color = textColor
|
color = textColor
|
||||||
textSize = normalTextSize
|
textSize = normalTextSize
|
||||||
textAlign = Paint.Align.CENTER
|
textAlign = Paint.Align.CENTER
|
||||||
}
|
}
|
||||||
|
|
||||||
weekDaysLetterHeight = 2 * normalTextSize.toInt()
|
|
||||||
initWeekDayLetters()
|
initWeekDayLetters()
|
||||||
setupCurrentDayOfWeekIndex()
|
setupCurrentDayOfWeekIndex()
|
||||||
}
|
}
|
||||||
|
@ -57,11 +58,11 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
|
||||||
override fun onDraw(canvas: Canvas) {
|
override fun onDraw(canvas: Canvas) {
|
||||||
super.onDraw(canvas)
|
super.onDraw(canvas)
|
||||||
if (dayWidth == 0f) {
|
if (dayWidth == 0f) {
|
||||||
dayWidth = (canvas.width / 7).toFloat()
|
dayWidth = canvas.width / 7f
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dayHeight == 0f) {
|
if (dayHeight == 0f) {
|
||||||
dayHeight = ((canvas.height - weekDaysLetterHeight) / 6).toFloat()
|
dayHeight = (canvas.height - weekDaysLetterHeight) / 6f
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i in 0..6) {
|
for (i in 0..6) {
|
||||||
|
@ -75,10 +76,10 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
|
||||||
|
|
||||||
var curId = 0
|
var curId = 0
|
||||||
for (y in 0..5) {
|
for (y in 0..5) {
|
||||||
for (x in 1..7) {
|
for (x in 0..6) {
|
||||||
val day = days.getOrNull(curId)
|
val day = days.getOrNull(curId)
|
||||||
if (day != null) {
|
if (day != null) {
|
||||||
val xPos = x * dayWidth - dayWidth / 2
|
val xPos = x * dayWidth + dayWidth / 2
|
||||||
val yPos = y * dayHeight + weekDaysLetterHeight
|
val yPos = y * dayHeight + weekDaysLetterHeight
|
||||||
if (day.isToday) {
|
if (day.isToday) {
|
||||||
canvas.drawCircle(xPos, yPos + paint.textSize * 0.7f, paint.textSize * 0.75f, getCirclePaint(day))
|
canvas.drawCircle(xPos, yPos + paint.textSize * 0.7f, paint.textSize * 0.75f, getCirclePaint(day))
|
||||||
|
|
|
@ -0,0 +1,88 @@
|
||||||
|
package com.simplemobiletools.calendar.views
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.util.AttributeSet
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.widget.FrameLayout
|
||||||
|
import com.simplemobiletools.calendar.R
|
||||||
|
import com.simplemobiletools.calendar.models.DayMonthly
|
||||||
|
import com.simplemobiletools.commons.extensions.onGlobalLayout
|
||||||
|
import kotlinx.android.synthetic.main.month_view.view.*
|
||||||
|
|
||||||
|
// used in the Monthly view fragment, 1 view per screen
|
||||||
|
class MonthViewWrapper(context: Context, attrs: AttributeSet, defStyle: Int) : FrameLayout(context, attrs, defStyle) {
|
||||||
|
private var dayWidth = 0f
|
||||||
|
private var dayHeight = 0f
|
||||||
|
private var weekDaysLetterHeight = 0
|
||||||
|
private var wereViewsAdded = false
|
||||||
|
private var days = ArrayList<DayMonthly>()
|
||||||
|
private var inflater: LayoutInflater
|
||||||
|
private var monthView: MonthView
|
||||||
|
|
||||||
|
constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)
|
||||||
|
|
||||||
|
init {
|
||||||
|
val normalTextSize = resources.getDimensionPixelSize(R.dimen.normal_text_size).toFloat()
|
||||||
|
weekDaysLetterHeight = 2 * normalTextSize.toInt()
|
||||||
|
|
||||||
|
inflater = LayoutInflater.from(context)
|
||||||
|
monthView = inflater.inflate(R.layout.month_view, this).month_view
|
||||||
|
|
||||||
|
onGlobalLayout {
|
||||||
|
measureSizes()
|
||||||
|
if (!wereViewsAdded && days.isNotEmpty()) {
|
||||||
|
addViews()
|
||||||
|
monthView.updateDays(days)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateDays(newDays: ArrayList<DayMonthly>) {
|
||||||
|
days = newDays
|
||||||
|
if (dayWidth != 0f) {
|
||||||
|
addViews()
|
||||||
|
monthView.updateDays(days)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun measureSizes() {
|
||||||
|
if (dayWidth == 0f) {
|
||||||
|
dayWidth = width / 7f
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dayHeight == 0f) {
|
||||||
|
dayHeight = (height - weekDaysLetterHeight) / 6f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun addViews() {
|
||||||
|
removeAllViews()
|
||||||
|
monthView = inflater.inflate(R.layout.month_view, this).month_view
|
||||||
|
wereViewsAdded = true
|
||||||
|
var curId = 0
|
||||||
|
for (y in 0..5) {
|
||||||
|
for (x in 0..6) {
|
||||||
|
val day = days.getOrNull(curId)
|
||||||
|
if (day != null) {
|
||||||
|
val xPos = x * dayWidth
|
||||||
|
val yPos = y * dayHeight + weekDaysLetterHeight
|
||||||
|
addViewBackground(xPos, yPos, day)
|
||||||
|
}
|
||||||
|
curId++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun addViewBackground(xPos: Float, yPos: Float, day: DayMonthly) {
|
||||||
|
inflater.inflate(R.layout.month_view_background, this, false).apply {
|
||||||
|
layoutParams.width = dayWidth.toInt()
|
||||||
|
layoutParams.height = dayHeight.toInt()
|
||||||
|
x = xPos
|
||||||
|
y = yPos
|
||||||
|
setOnClickListener {
|
||||||
|
|
||||||
|
}
|
||||||
|
addView(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,10 +8,10 @@
|
||||||
|
|
||||||
<include layout="@layout/top_navigation"/>
|
<include layout="@layout/top_navigation"/>
|
||||||
|
|
||||||
<com.simplemobiletools.calendar.views.MonthView
|
<com.simplemobiletools.calendar.views.MonthViewWrapper
|
||||||
android:id="@+id/month_view"
|
android:id="@+id/month_view_wrapper"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="match_parent"
|
||||||
android:layout_below="@+id/top_left_arrow"/>
|
android:layout_below="@+id/top_left_arrow"/>
|
||||||
|
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<com.simplemobiletools.calendar.views.MonthView
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/month_view"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"/>
|
|
@ -0,0 +1,7 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<View
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/month_view_background"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:background="?attr/selectableItemBackground"/>
|
Loading…
Reference in New Issue