Skip to content

Instantly share code, notes, and snippets.

@markchristopherng
Last active July 4, 2018 04:47
Show Gist options
  • Save markchristopherng/8824ba6b06ecfb79c745a51af59d3d40 to your computer and use it in GitHub Desktop.
Save markchristopherng/8824ba6b06ecfb79c745a51af59d3d40 to your computer and use it in GitHub Desktop.
BranchLocationPageAdapter
class BranchLocationPageAdapter(var context: Context) : PagedListAdapter<BranchLocation, BranchLocationPageAdapter.ViewHolder>(diffCallback), FastScrollRecyclerView.SectionedAdapter {
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
bindItem(holder, position)
}
fun getBranchLocation(position: Int) = getItem(position)
fun bindItem(holder: ViewHolder, position: Int) {
val obj = getItem(position)
if (obj != null) {
holder.itemView.group_shadow.visibility = View.GONE
holder.itemView.text_postcode_name.text = "${obj.locality} ${obj.state}".toUpperCase(Locale.ENGLISH)
var category = if ("Delivery Area".equals(obj.category!!, ignoreCase = true)) "" else obj.category
holder.itemView.text_postcode_category.text = category
holder.itemView.text_postcode.text = obj.postcode
holder.itemView.text_postcode_category.visibility = if (StringUtil.isEmpty(category)) View.GONE else View.VISIBLE
holder.itemView.view_divider.visibility = if (position == itemCount - 1) View.INVISIBLE else View.VISIBLE
} else {
holder.itemView.group_shadow.visibility = View.VISIBLE
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.find_postcode_item, parent, false)
return ViewHolder(itemView)
}
override fun getSectionName(position: Int): String {
val obj = getItem(position) ?: return ""
return obj.locality?.substring(0, 1) ?: ""
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
companion object {
private val diffCallback = object : DiffUtil.ItemCallback<BranchLocation>() {
override fun areItemsTheSame(oldItem: BranchLocation, newItem: BranchLocation): Boolean =
oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: BranchLocation, newItem: BranchLocation): Boolean =
oldItem == newItem
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment