From 952df754a454c2b38852f10358ffb2bb7a153fdf Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 15 Jan 2017 23:05:25 +0100 Subject: [PATCH] create a gridview viewholder --- .../calendar/adapters/MyWeekPagerAdapter.kt | 2 +- .../calendar/adapters/WeekEventsAdapter.kt | 20 ++++++++++++++++--- .../calendar/fragments/WeekFragment.kt | 3 ++- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyWeekPagerAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyWeekPagerAdapter.kt index 1d88ae55f..606e28ad4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyWeekPagerAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyWeekPagerAdapter.kt @@ -10,7 +10,7 @@ import com.simplemobiletools.calendar.fragments.WeekFragment class MyWeekPagerAdapter(fm: FragmentManager, private val mListener: WeekFragment.WeekScrollListener) : FragmentStatePagerAdapter(fm) { private val mFragments = SparseArray() - override fun getCount() = 3 + override fun getCount() = 1 override fun getItem(position: Int): Fragment { val bundle = Bundle() diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/WeekEventsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/WeekEventsAdapter.kt index dfab2043f..bb6408577 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/WeekEventsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/WeekEventsAdapter.kt @@ -6,17 +6,31 @@ import android.view.View import android.view.ViewGroup import android.widget.BaseAdapter import com.simplemobiletools.calendar.R +import com.simplemobiletools.calendar.models.Event -class WeekEventsAdapter(val context: Context) : BaseAdapter() { +class WeekEventsAdapter(val context: Context, val mEvents: List) : BaseAdapter() { private val mInflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { - return mInflater.inflate(R.layout.week_grid_item, parent, false) + var view = convertView + val viewHolder: ViewHolder + + if (view == null) { + view = mInflater.inflate(R.layout.week_grid_item, parent, false) + viewHolder = ViewHolder(view) + view!!.tag = viewHolder + } else { + viewHolder = view.tag as ViewHolder + } + + return view } - override fun getItem(position: Int) = null + override fun getItem(position: Int) = mEvents[position] override fun getItemId(position: Int) = 0L override fun getCount() = 24 * 7 + + internal class ViewHolder(view: View) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/WeekFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/WeekFragment.kt index 24420cb4a..219a144b9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/WeekFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/WeekFragment.kt @@ -15,6 +15,7 @@ import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.calendar.views.MyScrollView import kotlinx.android.synthetic.main.fragment_week.view.* import org.joda.time.DateTime +import java.util.* class WeekFragment : Fragment(), WeeklyCalendar { private var mListener: WeekScrollListener? = null @@ -37,7 +38,7 @@ class WeekFragment : Fragment(), WeeklyCalendar { } }) - mView.week_events_grid.adapter = WeekEventsAdapter(context) + mView.week_events_grid.adapter = WeekEventsAdapter(context, ArrayList()) mCalendar = WeeklyCalendarImpl(this, context) return mView