replace events listview with recyclerview, initial version
This commit is contained in:
parent
277343a32a
commit
84b5152e3e
|
@ -1,60 +1,83 @@
|
||||||
package com.simplemobiletools.calendar.adapters
|
package com.simplemobiletools.calendar.adapters
|
||||||
|
|
||||||
import android.content.Context
|
import android.support.v7.view.ActionMode
|
||||||
import android.view.LayoutInflater
|
import android.support.v7.widget.RecyclerView
|
||||||
import android.view.View
|
import android.view.*
|
||||||
import android.view.ViewGroup
|
import com.bignerdranch.android.multiselector.ModalMultiSelectorCallback
|
||||||
import android.widget.BaseAdapter
|
import com.bignerdranch.android.multiselector.MultiSelector
|
||||||
|
import com.bignerdranch.android.multiselector.SwappingHolder
|
||||||
import com.simplemobiletools.calendar.R
|
import com.simplemobiletools.calendar.R
|
||||||
|
import com.simplemobiletools.calendar.activities.SimpleActivity
|
||||||
import com.simplemobiletools.calendar.helpers.Formatter
|
import com.simplemobiletools.calendar.helpers.Formatter
|
||||||
import com.simplemobiletools.calendar.models.Event
|
import com.simplemobiletools.calendar.models.Event
|
||||||
import kotlinx.android.synthetic.main.event_item.view.*
|
import kotlinx.android.synthetic.main.event_item.view.*
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
class EventsAdapter(context: Context, private val mEvents: List<Event>) : BaseAdapter() {
|
class EventsAdapter(val activity: SimpleActivity, val mItems: List<Event>, val itemClick: (Event) -> Unit) :
|
||||||
private val mInflater: LayoutInflater
|
RecyclerView.Adapter<EventsAdapter.ViewHolder>() {
|
||||||
|
val multiSelector = MultiSelector()
|
||||||
|
val views = ArrayList<View>()
|
||||||
|
|
||||||
init {
|
companion object {
|
||||||
mInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
|
var actMode: ActionMode? = null
|
||||||
|
val markedItems = HashSet<Int>()
|
||||||
|
|
||||||
|
fun toggleItemSelection(itemView: View, select: Boolean, pos: Int = -1) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) {
|
||||||
var view = convertView
|
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
|
||||||
val viewHolder: ViewHolder
|
return when (item.itemId) {
|
||||||
if (view == null) {
|
else -> false
|
||||||
view = mInflater.inflate(R.layout.event_item, parent, false)
|
|
||||||
viewHolder = ViewHolder(view)
|
|
||||||
view!!.tag = viewHolder
|
|
||||||
} else {
|
|
||||||
viewHolder = view.tag as ViewHolder
|
|
||||||
}
|
|
||||||
|
|
||||||
val event = mEvents[position]
|
|
||||||
viewHolder.apply {
|
|
||||||
title.text = event.title
|
|
||||||
description.text = event.description
|
|
||||||
start.text = Formatter.getTime(event.startTS)
|
|
||||||
|
|
||||||
if (event.startTS == event.endTS) {
|
|
||||||
end.visibility = View.INVISIBLE
|
|
||||||
} else {
|
|
||||||
end.text = Formatter.getTime(event.endTS)
|
|
||||||
end.visibility = View.VISIBLE
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return view
|
override fun onCreateActionMode(actionMode: ActionMode?, menu: Menu?): Boolean {
|
||||||
|
super.onCreateActionMode(actionMode, menu)
|
||||||
|
actMode = actionMode
|
||||||
|
activity.menuInflater.inflate(R.menu.menu_day_cab, menu)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPrepareActionMode(actionMode: ActionMode?, menu: Menu): Boolean {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroyActionMode(actionMode: ActionMode?) {
|
||||||
|
super.onDestroyActionMode(actionMode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getCount() = mEvents.size
|
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
|
||||||
|
val view = LayoutInflater.from(parent?.context).inflate(R.layout.event_item, parent, false)
|
||||||
|
return ViewHolder(activity, view, itemClick)
|
||||||
|
}
|
||||||
|
|
||||||
override fun getItem(position: Int) = mEvents[position]
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||||
|
views.add(holder.bindView(multiSelectorMode, multiSelector, mItems[position], position))
|
||||||
|
}
|
||||||
|
|
||||||
override fun getItemId(position: Int) = 0L
|
override fun getItemCount() = mItems.size
|
||||||
|
|
||||||
class ViewHolder(view: View) {
|
class ViewHolder(val activity: SimpleActivity, view: View, val itemClick: (Event) -> (Unit)) : SwappingHolder(view, MultiSelector()) {
|
||||||
val title = view.event_item_title
|
fun bindView(multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, event: Event, pos: Int): View {
|
||||||
val description = view.event_item_description
|
|
||||||
val start = view.event_item_start
|
itemView.apply {
|
||||||
val end = view.event_item_end
|
event_item_title.text = event.title
|
||||||
|
event_item_description.text = event.description
|
||||||
|
event_item_start.text = Formatter.getTime(event.startTS)
|
||||||
|
|
||||||
|
if (event.startTS == event.endTS) {
|
||||||
|
event_item_end.visibility = View.INVISIBLE
|
||||||
|
} else {
|
||||||
|
event_item_end.text = Formatter.getTime(event.endTS)
|
||||||
|
event_item_end.visibility = View.VISIBLE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return itemView
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,11 +10,11 @@ import android.support.v4.app.Fragment
|
||||||
import android.support.v7.app.AlertDialog
|
import android.support.v7.app.AlertDialog
|
||||||
import android.view.*
|
import android.view.*
|
||||||
import android.widget.AbsListView
|
import android.widget.AbsListView
|
||||||
import android.widget.AdapterView
|
|
||||||
import android.widget.DatePicker
|
import android.widget.DatePicker
|
||||||
import android.widget.RelativeLayout
|
import android.widget.RelativeLayout
|
||||||
import com.simplemobiletools.calendar.R
|
import com.simplemobiletools.calendar.R
|
||||||
import com.simplemobiletools.calendar.activities.EventActivity
|
import com.simplemobiletools.calendar.activities.EventActivity
|
||||||
|
import com.simplemobiletools.calendar.activities.SimpleActivity
|
||||||
import com.simplemobiletools.calendar.adapters.EventsAdapter
|
import com.simplemobiletools.calendar.adapters.EventsAdapter
|
||||||
import com.simplemobiletools.calendar.extensions.adjustAlpha
|
import com.simplemobiletools.calendar.extensions.adjustAlpha
|
||||||
import com.simplemobiletools.calendar.helpers.*
|
import com.simplemobiletools.calendar.helpers.*
|
||||||
|
@ -26,7 +26,7 @@ import kotlinx.android.synthetic.main.top_navigation.view.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.comparisons.compareBy
|
import kotlin.comparisons.compareBy
|
||||||
|
|
||||||
class DayFragment : Fragment(), DBHelper.EventsListener, AdapterView.OnItemClickListener, AbsListView.MultiChoiceModeListener, DBHelper.GetEventsListener {
|
class DayFragment : Fragment(), DBHelper.EventsListener, AbsListView.MultiChoiceModeListener, DBHelper.GetEventsListener {
|
||||||
|
|
||||||
private val EDIT_EVENT = 1
|
private val EDIT_EVENT = 1
|
||||||
|
|
||||||
|
@ -127,11 +127,11 @@ class DayFragment : Fragment(), DBHelper.EventsListener, AdapterView.OnItemClick
|
||||||
if (activity == null)
|
if (activity == null)
|
||||||
return
|
return
|
||||||
|
|
||||||
val eventsAdapter = EventsAdapter(activity.baseContext, eventsToShow)
|
val eventsAdapter = EventsAdapter(activity as SimpleActivity, eventsToShow) {
|
||||||
|
|
||||||
|
}
|
||||||
mHolder.day_events.apply {
|
mHolder.day_events.apply {
|
||||||
adapter = eventsAdapter
|
this@apply.adapter = eventsAdapter
|
||||||
onItemClickListener = this@DayFragment
|
|
||||||
setMultiChoiceModeListener(this@DayFragment)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,13 +156,13 @@ class DayFragment : Fragment(), DBHelper.EventsListener, AdapterView.OnItemClick
|
||||||
private fun getEventsToShow(events: MutableList<Event>) = events.filter { !mToBeDeleted.contains(it.id) }
|
private fun getEventsToShow(events: MutableList<Event>) = events.filter { !mToBeDeleted.contains(it.id) }
|
||||||
|
|
||||||
private fun prepareDeleteEvents() {
|
private fun prepareDeleteEvents() {
|
||||||
val checked = mHolder.day_events.checkedItemPositions
|
/*val checked = mHolder.day_events.checkedItemPositions
|
||||||
mEvents!!.indices
|
mEvents!!.indices
|
||||||
.filter { checked.get(it) }
|
.filter { checked.get(it) }
|
||||||
.map { mEvents!![it] }
|
.map { mEvents!![it] }
|
||||||
.forEach { mToBeDeleted.add(it.id) }
|
.forEach { mToBeDeleted.add(it.id) }
|
||||||
|
|
||||||
notifyDeletion()
|
notifyDeletion()*/
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun notifyDeletion() {
|
private fun notifyDeletion() {
|
||||||
|
@ -214,9 +214,9 @@ class DayFragment : Fragment(), DBHelper.EventsListener, AdapterView.OnItemClick
|
||||||
mode.invalidate()
|
mode.invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
|
/*override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
|
||||||
editEvent(getEventsToShow(mEvents!!)[position].id)
|
editEvent(getEventsToShow(mEvents!!)[position].id)
|
||||||
}
|
}*/
|
||||||
|
|
||||||
override fun eventInserted(event: Event) {
|
override fun eventInserted(event: Event) {
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,20 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<RelativeLayout
|
<RelativeLayout
|
||||||
android:id="@+id/day_holder"
|
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:id="@+id/day_holder"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content">
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
<include layout="@layout/top_navigation"/>
|
<include layout="@layout/top_navigation"/>
|
||||||
|
|
||||||
<ListView
|
<android.support.v7.widget.RecyclerView
|
||||||
android:id="@+id/day_events"
|
android:id="@+id/day_events"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_below="@+id/top_value"
|
android:layout_below="@+id/top_value"
|
||||||
android:choiceMode="multipleChoiceModal"
|
|
||||||
android:clipToPadding="false"
|
android:clipToPadding="false"
|
||||||
android:paddingLeft="@dimen/activity_margin"/>
|
android:paddingLeft="@dimen/activity_margin"
|
||||||
|
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
|
||||||
|
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
|
|
Loading…
Reference in New Issue