allow clicking and selecting events

This commit is contained in:
tibbi 2016-11-23 20:11:11 +01:00
parent 84b5152e3e
commit cb7d83a696
4 changed files with 43 additions and 11 deletions

View File

@ -23,7 +23,13 @@ class EventsAdapter(val activity: SimpleActivity, val mItems: List<Event>, val i
val markedItems = HashSet<Int>()
fun toggleItemSelection(itemView: View, select: Boolean, pos: Int = -1) {
if (pos == -1)
return
if (select)
markedItems.add(pos)
else
markedItems.remove(pos)
}
}
@ -37,16 +43,16 @@ class EventsAdapter(val activity: SimpleActivity, val mItems: List<Event>, val i
override fun onCreateActionMode(actionMode: ActionMode?, menu: Menu?): Boolean {
super.onCreateActionMode(actionMode, menu)
actMode = actionMode
activity.menuInflater.inflate(R.menu.menu_day_cab, menu)
activity.menuInflater.inflate(R.menu.cab_day, menu)
return true
}
override fun onPrepareActionMode(actionMode: ActionMode?, menu: Menu): Boolean {
return true
}
override fun onPrepareActionMode(actionMode: ActionMode?, menu: Menu) = true
override fun onDestroyActionMode(actionMode: ActionMode?) {
super.onDestroyActionMode(actionMode)
views.forEach { toggleItemSelection(it, false) }
markedItems.clear()
}
}
@ -75,9 +81,39 @@ class EventsAdapter(val activity: SimpleActivity, val mItems: List<Event>, val i
event_item_end.text = Formatter.getTime(event.endTS)
event_item_end.visibility = View.VISIBLE
}
setOnClickListener { viewClicked(multiSelector, event, pos) }
setOnLongClickListener {
if (!multiSelector.isSelectable) {
activity.startSupportActionMode(multiSelectorCallback)
multiSelector.setSelected(this@ViewHolder, true)
actMode?.title = multiSelector.selectedPositions.size.toString()
toggleItemSelection(itemView, true, pos)
actMode?.invalidate()
}
true
}
}
return itemView
}
fun viewClicked(multiSelector: MultiSelector, event: Event, pos: Int) {
if (multiSelector.isSelectable) {
val isSelected = multiSelector.selectedPositions.contains(layoutPosition)
multiSelector.setSelected(this, !isSelected)
toggleItemSelection(itemView, !isSelected, pos)
val selectedCnt = multiSelector.selectedPositions.size
if (selectedCnt == 0) {
actMode?.finish()
} else {
actMode?.title = selectedCnt.toString()
}
actMode?.invalidate()
} else {
itemClick(event)
}
}
}
}

View File

@ -128,7 +128,7 @@ class DayFragment : Fragment(), DBHelper.EventsListener, AbsListView.MultiChoice
return
val eventsAdapter = EventsAdapter(activity as SimpleActivity, eventsToShow) {
editEvent(it.id)
}
mHolder.day_events.apply {
this@apply.adapter = eventsAdapter
@ -195,7 +195,7 @@ class DayFragment : Fragment(), DBHelper.EventsListener, AbsListView.MultiChoice
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
val inflater = mode.menuInflater
inflater.inflate(R.menu.menu_day_cab, menu)
inflater.inflate(R.menu.cab_day, menu)
return true
}
@ -214,10 +214,6 @@ class DayFragment : Fragment(), DBHelper.EventsListener, AbsListView.MultiChoice
mode.invalidate()
}
/*override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
editEvent(getEventsToShow(mEvents!!)[position].id)
}*/
override fun eventInserted(event: Event) {
}

View File

@ -163,7 +163,7 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, AdapterView.On
override fun onCreateActionMode(mode: ActionMode, menu: Menu?): Boolean {
val inflater = mode.menuInflater
inflater.inflate(R.menu.menu_day_cab, menu)
inflater.inflate(R.menu.cab_day, menu)
return true
}