This commit is contained in:
lunaticbum 2024-11-26 17:55:53 +09:00
parent 4226235569
commit 29d47c1162
5 changed files with 91 additions and 23 deletions

View File

@ -1,6 +1,7 @@
package com.example.calrendarview package com.example.calrendarview
import android.content.Context import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet import android.util.AttributeSet
import android.util.Log import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
@ -9,6 +10,7 @@ import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
abstract class BaseCustomViews : ConstraintLayout { abstract class BaseCustomViews : ConstraintLayout {
protected val TAG = this:: class.simpleName
constructor(context: Context) : super(context){appyAttrs(context,null)} constructor(context: Context) : super(context){appyAttrs(context,null)}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs){appyAttrs(context,attrs)} constructor(context: Context, attrs: AttributeSet?) : super(context, attrs){appyAttrs(context,attrs)}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super( constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
@ -27,12 +29,15 @@ abstract class BaseCustomViews : ConstraintLayout {
var layouId : Int = -1 var layouId : Int = -1
open fun appyAttrs(context: Context?, attrs: AttributeSet?) { open fun appyAttrs(context: Context?, attrs: AttributeSet?) {
context?.let { context?.let {
val typedArray = it.obtainStyledAttributes(attrs, R.styleable.BaseCustomViews) onTypedArray(it,it.obtainStyledAttributes(attrs, R.styleable.BaseCustomViews))
layouId = typedArray.getResourceId(R.styleable.BaseCustomViews_item_layout,R.layout.item_day)
initLayout(it)
} }
} }
open fun onTypedArray(context: Context, typedArray : TypedArray) {
layouId = typedArray.getResourceId(R.styleable.BaseCustomViews_item_layout,R.layout.item_day)
initLayout(context)
}
fun initLayout(context: Context?) { fun initLayout(context: Context?) {
createdLayout(LayoutInflater.from(context).inflate(layouId, this, true)) createdLayout(LayoutInflater.from(context).inflate(layouId, this, true))
} }

View File

@ -1,24 +1,79 @@
package com.example.calrendarview package com.example.calrendarview
import android.content.Context import android.content.Context
import android.content.res.TypedArray
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Color import android.graphics.Color
import android.util.AttributeSet import android.util.AttributeSet
import android.util.Log
import android.view.View import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.view.children
import com.example.calrendarview.model.DayInfo
class DayView : BaseCustomViews { class DayView : BaseCustomViews {
constructor(context: Context) : super(context) constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) context,
attrs,
defStyleAttr
)
constructor(
context: Context,
attrs: AttributeSet?,
defStyleAttr: Int,
defStyleRes: Int
) : super(context, attrs, defStyleAttr, defStyleRes)
override fun appyAttrs(context: Context?, attrs: AttributeSet?) { var dayInfo: DayInfo? = null
super.appyAttrs(context, attrs) set(value) {
field = value
displayDayNumber()
}
var numOfDayView : TextView? = null
var numberOfday : Int = -1
set(value) {
Log.e(TAG, "numOfDayView 2 (${R.id.day_num}) Id >>>> ${value}")
field = value
numOfDayView = findViewById<TextView>(field)
Log.e(TAG, "numOfDayView >>>> 3 ${numOfDayView}")
}
// override fun appyAttrs(context: Context?, attrs: AttributeSet?) {
// context?.let {
// Log.e(TAG, "numOfDayView >>>> ${numOfDayView}")
// var typedArray = it.obtainStyledAttributes(attrs, R.styleable.BaseCustomViews)
// layouId = typedArray.getResourceId(R.styleable.BaseCustomViews_item_layout,R.layout.item_day).apply {
// Log.e(TAG, "layouId 0 (${R.layout.item_day} Id >>>> ${this}")
// }
// initLayout(context)
//
// }
// }
override fun onTypedArray(context: Context, typedArray: TypedArray) {
super.onTypedArray(context, typedArray)
numberOfday = typedArray.getResourceId(R.styleable.BaseCustomViews_numOfDayId, R.id.day_num).apply {
Log.e(TAG, "numOfDayView 1 (${R.id.day_num}) Id >>>> ${this}")
}
displayDayNumber()
}
override fun createdLayout(view: View) {
super.createdLayout(view)
(view as? ViewGroup)?.let {
it.children.forEach { Log.e(TAG,"child is ${it}") }
}
} }
fun displayDayNumber() { fun displayDayNumber() {
//todo 오늘 의 날짜를 표시 numOfDayView?.text = dayInfo?.getDayOfNum()?.toString() ?: "TEST"
Log.e(TAG, "numOfDayView?.text >>>> 4 ${numOfDayView?.text}")
} }
fun displayDayOfWeek() { fun displayDayOfWeek() {
@ -29,11 +84,14 @@ class DayView : BaseCustomViews {
return false return false
} }
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
override fun draw(canvas: Canvas) { super.onMeasure(widthMeasureSpec, heightMeasureSpec)
canvas.drawColor(Color.CYAN) Log.e(TAG,"View >>> ${this}")
super.draw(canvas)
} }
// override fun draw(canvas: Canvas) {
// super.draw(canvas)
// canvas.drawColor(Color.CYAN)
// }
} }

View File

@ -2,7 +2,7 @@ package com.example.calrendarview.model
interface DayInfo { interface DayInfo {
fun getDayOfNum() fun getDayOfNum() : Int
fun getDayOfWeek() fun getDayOfWeek() : Int
fun isToday() fun isToday() : Boolean
} }

View File

@ -3,12 +3,15 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/day_num"
android:gravity="right|top"
android:textSize="20dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:src="@android:drawable/btn_dialog"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_width="200dp"
android:layout_height="200dp"/>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -2,14 +2,16 @@
<resources> <resources>
<attr name="item_layout" format="reference"/> <attr name="item_layout" format="reference"/>
<attr name="numOfDayId" format="reference"/>
<declare-styleable name="BaseCustomViews"> <declare-styleable name="BaseCustomViews">
<attr name="item_layout"/> <attr name="item_layout"/>
<attr name="numOfDayId"/>
</declare-styleable> </declare-styleable>
<declare-styleable name="DayView"> <declare-styleable name="DayView">
<attr name="item_layout"/> <attr name="item_layout"/>
<attr name="numOfDayId"/>
</declare-styleable> </declare-styleable>
<declare-styleable name="MonthView"> <declare-styleable name="MonthView">