Skip to content

Instantly share code, notes, and snippets.

@brh
Created August 4, 2020 17:05
Show Gist options
  • Save brh/15269115fdd8ab26a5c697adca24b5d8 to your computer and use it in GitHub Desktop.
Save brh/15269115fdd8ab26a5c697adca24b5d8 to your computer and use it in GitHub Desktop.
class StepDataAdapter(var stepList : List<StepData>) : RecyclerView.Adapter<StepDataVH>() {
var mirrorList : List<StepData>? = null
override fun getItemViewType(position: Int): Int = when (position) {
0 -> HEADER
else -> DATA
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StepDataVH {
val inflater = LayoutInflater.from(parent.context)
return when (viewType) {
HEADER -> {
StepDataVH(ItemHeaderBinding.inflate(inflater, parent, false))
}
else -> {
StepDataVH(ItemStepDataBinding.inflate(inflater, parent, false))
}
}
}
override fun getItemCount(): Int {
return stepList.size + 1
}
override fun onBindViewHolder(holder: StepDataVH, position: Int) {
when (position) {
0 -> {
val binding = holder.binder as ItemHeaderBinding
binding.tvHeaderDate.setOnClickListener {
//if mirrorlist is null switch to descending else ascending
mirrorList?.let {
mirrorList = null
binding.tvHeaderDate.text = FitApp.ctx.getString(R.string.date_desc)
} ?: run {
mirrorList = stepList.reversed()
binding.tvHeaderDate.text = FitApp.ctx.getString(R.string.date)
}
//just notify the actual list data not the header, nanoseconds saved
notifyItemRangeChanged(1, stepList!!.size+1)
}
}
else -> {
val binding = holder.binder as ItemStepDataBinding
//if mirrorList is active use it
val item = (mirrorList ?: stepList)[position-1]
binding.tvDate.text = DateFormat.getMediumDateFormat(binding.root.context).format(item.date)
binding.tvSteps.text = item.steps.toString()
}
}
}
}
class StepDataVH(var binder : ViewBinding) : RecyclerView.ViewHolder(binder.root) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment