Skip to content

Commit 91e89c7

Browse files
committed
Reyclerview added and other modification done.
1 parent fa6cef0 commit 91e89c7

File tree

11 files changed

+219
-143
lines changed

11 files changed

+219
-143
lines changed

CalendarView/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ dependencies {
2020
implementation fileTree(dir: 'libs', include: ['*.jar'])
2121
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
2222
implementation 'androidx.core:core-ktx:1.3.2'
23+
implementation 'com.google.android.material:material:1.3.0'
2324
}

CalendarView/src/main/java/com/shahzadafridi/calendarview/CalendarAdapter.kt

Lines changed: 99 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ import android.graphics.Color
55
import android.graphics.Typeface
66
import android.view.LayoutInflater
77
import android.view.View
8-
import android.view.ViewDebug
98
import android.view.ViewGroup
10-
import android.widget.ArrayAdapter
9+
import android.widget.RelativeLayout
1110
import android.widget.TextView
1211
import androidx.core.content.ContextCompat
1312
import androidx.core.content.res.ResourcesCompat
13+
import androidx.recyclerview.widget.RecyclerView
1414
import java.util.*
1515

1616
class CalendarAdapter(
17-
context: Context,
18-
days: ArrayList<Calendar>, // days with events
19-
private val eventDays: HashSet<Calendar>?
20-
) : ArrayAdapter<Calendar>(context, R.layout.control_calendar_day, days) {
17+
context: Context,
18+
private val days: ArrayList<Calendar>, // days with events
19+
private val eventDays: HashSet<Calendar>?,
20+
private var eventsHandler: CalenderViewInterface.EventHandler?,
21+
private var cellConfig: CellConfiguration?
22+
) : RecyclerView.Adapter<CalendarAdapter.MyViewHolder>() {
2123

2224
// for view inflation
2325
private val inflater: LayoutInflater
@@ -35,80 +37,108 @@ class CalendarAdapter(
3537
init {
3638
inflater = LayoutInflater.from(context)
3739
mContext = context
40+
cellConfig?.let {
41+
cell_font = it.cellFont
42+
cell_size = it.cellSize
43+
cell_bg = it.cellBg
44+
cell_txt_clr = it.cellTxtClr
45+
cell_txt_size = it.cellTxtSize
46+
cell_selected_txt_clr = it.cellSelectedClr
47+
cell_select_bg = it.cellSelectBg
48+
}
3849
}
3950

40-
override fun getView(position: Int, view: View?, parent: ViewGroup): View {
41-
42-
// day in question
43-
var view = view
44-
val date = getItem(position)
45-
46-
val day = date!!.get(Calendar.DATE)
47-
val month = date.get(Calendar.MONTH)
48-
val year = date.get(Calendar.YEAR)
49-
50-
// today
51-
val today = Calendar.getInstance()
52-
53-
// inflate item if it does not exist yet
54-
if (view == null) view = inflater.inflate(R.layout.control_calendar_day, parent, false)
55-
56-
var textView = view as TextView
57-
// clear styling
58-
textView.setTypeface(null, Typeface.NORMAL)
59-
textView.setTextColor(Color.BLACK)
60-
if (month != today.get(Calendar.MONTH) || year != today.get(Calendar.YEAR)) {
61-
// if this day is outside current month, grey it out
62-
textView.setTextColor(ContextCompat.getColor(mContext, R.color.greyed_out))
63-
} else if (day == today.get(Calendar.DATE)) {
64-
// if it is today, set it to blue/bold
65-
textView.setTypeface(null, Typeface.BOLD)
66-
textView.setTextColor(ContextCompat.getColor(mContext, R.color.today))
67-
}
51+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CalendarAdapter.MyViewHolder {
52+
val view = LayoutInflater.from(mContext).inflate(R.layout.row_calendar_day_layout, parent, false)
53+
return MyViewHolder(view)
54+
}
6855

69-
cell_txt_size?.let {
70-
textView.textSize = it.toFloat()
71-
}
72-
cell_txt_clr?.let {
73-
textView.setTextColor(ContextCompat.getColor(mContext, it))
74-
}
75-
cell_font?.let {
76-
textView.typeface = ResourcesCompat.getFont(context, it)
77-
}
78-
// set text
79-
textView.text = date.get(Calendar.DATE).toString()
56+
override fun onBindViewHolder(holder: CalendarAdapter.MyViewHolder, position: Int) {
57+
holder.updateUi(holder,days[position])
58+
}
8059

81-
// if this day has an event, specify event image
82-
cell_bg?.let {
83-
view.setBackgroundResource(it)
60+
override fun getItemCount(): Int {
61+
return days.size
62+
}
63+
64+
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener, View.OnLongClickListener {
65+
66+
var textView: TextView
67+
var rowLayout: RelativeLayout
68+
69+
init {
70+
textView = itemView.findViewById(R.id.control_calendar_day)
71+
rowLayout = itemView.findViewById(R.id.row_calendar_layout)
72+
rowLayout.setOnClickListener(this)
73+
rowLayout.setOnLongClickListener(this)
74+
cell_txt_size?.let {
75+
textView.textSize = it.toFloat()
76+
}
77+
cell_txt_clr?.let {
78+
textView.setTextColor(ContextCompat.getColor(mContext, it))
79+
}
80+
cell_font?.let {
81+
textView.typeface = ResourcesCompat.getFont(mContext, it)
82+
}
83+
cell_bg?.let {
84+
rowLayout.setBackgroundResource(it)
85+
}
8486
}
8587

86-
if (eventDays != null) {
87-
for (eventDate in eventDays) {
88-
if (eventDate.get(Calendar.DATE) == day && eventDate.get(Calendar.MONTH) == month && eventDate.get(Calendar.YEAR) == year) {
89-
// mark this day for event
90-
cell_select_bg?.let {
91-
view.setBackgroundResource(it)
92-
}
93-
cell_selected_txt_clr?.let {
94-
textView.setTextColor(ContextCompat.getColor(mContext, it))
88+
fun updateUi(holder: MyViewHolder, date: Calendar) {
89+
90+
val day = date.get(Calendar.DATE)
91+
val month = date.get(Calendar.MONTH)
92+
val year = date.get(Calendar.YEAR)
93+
94+
// today
95+
val today = Calendar.getInstance()
96+
97+
// set text
98+
holder.textView.text = date.get(Calendar.DATE).toString()
99+
100+
101+
if (month != today.get(Calendar.MONTH) || year != today.get(Calendar.YEAR)) {
102+
// if this day is outside current month, grey it out
103+
holder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.greyed_out))
104+
} else if (day == today.get(Calendar.DATE)) {
105+
// if it is today, set it to blue/bold
106+
cell_selected_txt_clr?.let {
107+
holder.textView.setTextColor(ContextCompat.getColor(mContext, it))
108+
}
109+
cell_select_bg?.let {
110+
holder.rowLayout.setBackgroundResource(it)
111+
}
112+
}
113+
114+
if (eventDays != null) {
115+
for (eventDate in eventDays) {
116+
if (eventDate.get(Calendar.DATE) == day && eventDate.get(Calendar.MONTH) == month && eventDate.get(Calendar.YEAR) == year) {
117+
// mark this day for event
118+
cell_select_bg?.let {
119+
holder.rowLayout.setBackgroundResource(it)
120+
}
121+
cell_selected_txt_clr?.let {
122+
holder.textView.setTextColor(ContextCompat.getColor(mContext, it))
123+
}
124+
break
95125
}
96-
break
97126
}
98127
}
99128
}
100129

101-
return view
130+
override fun onClick(v: View?) {
131+
eventsHandler?.onCellClick(view = v,date = days[adapterPosition].time,adapterPosition)
132+
}
133+
134+
override fun onLongClick(v: View?): Boolean {
135+
eventsHandler?.onCellLongClick(view = v,date = days[adapterPosition].time,adapterPosition)
136+
return true
137+
}
102138
}
103139

104-
fun setCellConfig(cellTxtClr: Int?, cellBg: Int?, cellSelectedClr: Int?, cellSelectBg: Int?, cellFont: Int?, cellSize: Int?, cellTxtSize: Int?) {
105-
cell_txt_clr = cellTxtClr
106-
cell_bg = cellBg
107-
cell_selected_txt_clr = cellSelectedClr
108-
cell_select_bg = cellSelectBg
109-
cell_font = cellFont
110-
cell_size = cellSize
111-
cell_txt_size = cellTxtSize
112-
notifyDataSetChanged()
140+
fun setEventHandler(mEventsHandler: CalenderViewInterface.EventHandler){
141+
this.eventsHandler = mEventsHandler
113142
}
114-
}
143+
144+
}

CalendarView/src/main/java/com/shahzadafridi/calendarview/CalendarView.kt

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import android.content.Context
44
import android.util.AttributeSet
55
import android.view.LayoutInflater
66
import android.view.View
7-
import android.view.ViewGroup
87
import android.widget.*
9-
import android.widget.AdapterView.OnItemLongClickListener
10-
import android.widget.AdapterView.OnItemClickListener
118
import androidx.core.content.ContextCompat
129
import androidx.core.content.res.ResourcesCompat
1310
import androidx.core.view.children
11+
import androidx.recyclerview.widget.GridLayoutManager
12+
import androidx.recyclerview.widget.RecyclerView
1413
import java.text.SimpleDateFormat
1514
import java.util.*
1615

@@ -52,8 +51,9 @@ class CalendarView : LinearLayout, CalenderViewInterface {
5251
private var btnNext: ImageView? = null
5352
private var txtDate: TextView? = null
5453
private var dateRl: RelativeLayout? = null
55-
private var grid: GridView? = null
54+
private var recyclerView: RecyclerView? = null
5655
private var adapter: CalendarAdapter? = null
56+
private var cellConfig: CellConfiguration? = null
5757

5858
// seasons' rainbow
5959
var rainbow = intArrayOf(
@@ -175,7 +175,9 @@ class CalendarView : LinearLayout, CalenderViewInterface {
175175
btnNext = findViewById<View>(R.id.calendar_next_button) as ImageView
176176
txtDate = findViewById<View>(R.id.calendar_date_display) as TextView
177177
dateRl = findViewById<RelativeLayout>(R.id.calendar_date_rl)
178-
grid = findViewById<View>(R.id.calendar_grid) as GridView
178+
recyclerView = findViewById<View>(R.id.calendar_recyclerView) as RecyclerView
179+
recyclerView!!.setHasFixedSize(true)
180+
recyclerView!!.layoutManager = GridLayoutManager(context,7)
179181
}
180182

181183
private fun assignClickHandlers() {
@@ -192,18 +194,6 @@ class CalendarView : LinearLayout, CalenderViewInterface {
192194
updateCalendar()
193195
eventHandler!!.onPreviousClick(it)
194196
}
195-
196-
// long-pressing a day
197-
grid!!.onItemLongClickListener = OnItemLongClickListener { view, cell, position, id -> // handle long-press
198-
if (eventHandler == null) return@OnItemLongClickListener false
199-
eventHandler!!.onCellLongClick(view, view.getItemAtPosition(position) as Date, position, id)
200-
true
201-
}
202-
203-
grid!!.onItemClickListener = OnItemClickListener { view, cell, position, id ->
204-
if (eventHandler == null) return@OnItemClickListener
205-
eventHandler!!.onCellClick(view, view.getItemAtPosition(position) as Date, position, id)
206-
}
207197
}
208198

209199
/**
@@ -215,23 +205,23 @@ class CalendarView : LinearLayout, CalenderViewInterface {
215205
val calendar = currentDate.clone() as Calendar
216206

217207
// determine the cell for current month's beginning
218-
calendar[Calendar.DAY_OF_MONTH] = 1
208+
calendar[Calendar.DATE] = 1
219209
val monthBeginningCell = calendar[Calendar.DAY_OF_WEEK] - 1
220210

221211
// move calendar backwards to the beginning of the week
222-
calendar.add(Calendar.DAY_OF_MONTH, -monthBeginningCell)
212+
calendar.add(Calendar.DATE, -monthBeginningCell)
223213

224214
// fill cells
225215
while (cells.size < DAYS_COUNT) {
226216
cells.add(Calendar.getInstance().apply {
227217
this.time = calendar.time
228218
})
229-
calendar.add(Calendar.DAY_OF_MONTH, 1)
219+
calendar.add(Calendar.DATE, 1)
230220
}
231221

232222
// update grid
233-
adapter = CalendarAdapter(context, cells, events)
234-
grid!!.adapter = adapter
223+
adapter = CalendarAdapter(context, cells, events,eventHandler,cellConfig)
224+
recyclerView!!.adapter = adapter
235225

236226
// update title
237227
val sdf = SimpleDateFormat(headerDateFormat)
@@ -251,6 +241,7 @@ class CalendarView : LinearLayout, CalenderViewInterface {
251241
*/
252242
fun setEventHandler(eventHandler: CalenderViewInterface.EventHandler?) {
253243
this.eventHandler = eventHandler
244+
adapter!!.setEventHandler(eventHandler!!)
254245
}
255246

256247

@@ -341,6 +332,7 @@ class CalendarView : LinearLayout, CalenderViewInterface {
341332
textSize: Int,
342333
selectedTextColor: Int,
343334
selectedBackground: Int,
335+
cellSize: Int?,
344336
background: Int?
345337
): CalendarView {
346338
cell_font = font
@@ -349,7 +341,15 @@ class CalendarView : LinearLayout, CalenderViewInterface {
349341
cell_selected_txt_clr = selectedTextColor
350342
cell_select_bg = selectedBackground
351343
cell_bg = background
352-
adapter!!.setCellConfig(cell_txt_clr,cell_bg,cell_selected_txt_clr,cell_select_bg,cell_font,cell_size,cell_txt_size)
344+
cellConfig = CellConfiguration(
345+
cellTxtClr = cell_txt_clr,
346+
cellBg = cell_bg,
347+
cellSelectedClr = cell_selected_txt_clr,
348+
cellSelectBg = cell_select_bg,
349+
cellFont = cell_font,
350+
cellTxtSize = cell_txt_size,
351+
cellSize = cellSize
352+
)
353353
return this
354354
}
355355

@@ -359,7 +359,7 @@ class CalendarView : LinearLayout, CalenderViewInterface {
359359
left: Int,
360360
right: Int
361361
): CalendarView {
362-
setMargin(grid!!, left, right, top, bottom)
362+
setMargin(recyclerView!!, left, right, top, bottom)
363363
return this
364364
}
365365

CalendarView/src/main/java/com/shahzadafridi/calendarview/CalenderViewInterface.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ interface CalenderViewInterface {
99
fun withHeaderPanleMargin(top: Int = 0,bottom: Int = 0,left: Int = 0,right: Int = 0): CalendarView
1010
fun withDayPanel(font: Int?, textColor: Int?, background: Int?): CalendarView
1111
fun withDayPanelMargin(top: Int = 0,bottom: Int = 0,left: Int = 0,right: Int = 0): CalendarView
12-
fun withCellPanel(font: Int?, textColor: Int?, textSize: Int, selectedTextColor: Int, selectedBackground:Int, background: Int?): CalendarView
12+
fun withCellPanel(font: Int?, textColor: Int?, textSize: Int, selectedTextColor: Int, selectedBackground:Int, cellSize: Int?, background: Int?): CalendarView
1313
fun withCellPanelMargin(top: Int = 0,bottom: Int = 0,left: Int = 0,right: Int = 0): CalendarView
1414
fun withCalenderViewBackground(background: Int?): CalendarView
1515
fun build(): CalendarView
1616
interface EventHandler{
17-
fun onCellClick(view: View,date: Date, position: Int, id: Long)
18-
fun onCellLongClick(view: View, date: Date, position: Int, id: Long)
17+
fun onCellClick(view: View?, date: Date, position: Int)
18+
fun onCellLongClick(view: View?, date: Date, position: Int)
1919
fun onNextClick(view: View)
2020
fun onPreviousClick(view: View)
2121
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.shahzadafridi.calendarview
2+
3+
data class CellConfiguration(
4+
var cellTxtClr: Int? = null,
5+
var cellBg: Int? = null,
6+
var cellSelectedClr: Int? = null,
7+
var cellSelectBg: Int? = null,
8+
var cellFont: Int? = null,
9+
var cellSize: Int? = null,
10+
var cellTxtSize: Int? = null,
11+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:color="@color/cblack">
4+
5+
<item
6+
android:id="@android:id/mask"
7+
android:drawable="@android:color/white" />
8+
9+
</ripple>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:state_pressed="true">
4+
<shape android:shape="rectangle">
5+
<solid android:color="@color/cblack" />
6+
</shape>
7+
</item>
8+
<item>
9+
<shape android:shape="rectangle">
10+
<solid android:color="@android:color/transparent" />
11+
</shape>
12+
</item>
13+
</selector>

0 commit comments

Comments
 (0)