BaseViewHolder class added

This commit is contained in:
gouri-panda 2020-09-01 16:12:22 +05:30
parent dc802b1b3e
commit 3cf6d7bb63

View File

@ -22,17 +22,23 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.item_help.item_help_description
import kotlinx.android.synthetic.main.item_help.item_help_title
import kotlinx.android.synthetic.main.item_help.item_help_toggle_expand
import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.utils.AnimationUtils.collapse import org.kiwix.kiwixmobile.core.utils.AnimationUtils.collapse
import org.kiwix.kiwixmobile.core.utils.AnimationUtils.expand import org.kiwix.kiwixmobile.core.utils.AnimationUtils.expand
import kotlinx.android.synthetic.main.item_help.view.item_help_description import org.kiwix.kiwixmobile.core.base.adapter.BaseViewHolder
import kotlinx.android.synthetic.main.item_help.view.item_help_title
import kotlinx.android.synthetic.main.item_help.view.item_help_toggle_expand
internal class HelpAdapter(titleDescriptionMap: Map<String, String>) : internal class HelpAdapter(titleDescriptionMap: Map<String, String>) :
RecyclerView.Adapter<HelpAdapter.Item>() { RecyclerView.Adapter<HelpAdapter.Item>() {
private var titles: Array<String> = titleDescriptionMap.keys.toTypedArray() private var helpItems: ArrayList<HelpItem> = ArrayList()
private var descriptions: Array<String> = titleDescriptionMap.values.toTypedArray()
init {
titleDescriptionMap.forEach { (key, value) ->
helpItems.add(HelpItem(key, value))
}
}
override fun onCreateViewHolder( override fun onCreateViewHolder(
parent: ViewGroup, parent: ViewGroup,
@ -46,28 +52,32 @@ internal class HelpAdapter(titleDescriptionMap: Map<String, String>) :
holder: Item, holder: Item,
position: Int position: Int
) { ) {
holder.itemView.item_help_description.text = descriptions[position] holder.bind(helpItems[position])
holder.itemView.item_help_title.text = titles[position]
} }
override fun getItemCount(): Int = titles.size override fun getItemCount(): Int = helpItems.size
internal inner class Item(itemView: View) : internal inner class Item(itemView: View) :
RecyclerView.ViewHolder(itemView) { BaseViewHolder<HelpItem>(itemView) {
init {
itemView.item_help_title.setOnClickListener { toggleDescriptionVisibility() }
itemView.item_help_toggle_expand.setOnClickListener { toggleDescriptionVisibility() }
}
@SuppressWarnings("MagicNumber") @SuppressWarnings("MagicNumber")
fun toggleDescriptionVisibility() { fun toggleDescriptionVisibility() {
if (itemView.item_help_description.visibility == View.GONE) { if (item_help_description.visibility == View.GONE) {
ObjectAnimator.ofFloat(itemView.item_help_toggle_expand, "rotation", 0f, 180f).start() ObjectAnimator.ofFloat(item_help_toggle_expand, "rotation", 0f, 180f).start()
itemView.item_help_description.expand() item_help_description.expand()
} else { } else {
ObjectAnimator.ofFloat(itemView.item_help_toggle_expand, "rotation", 180f, 360f).start() ObjectAnimator.ofFloat(item_help_toggle_expand, "rotation", 180f, 360f).start()
itemView.item_help_description.collapse() item_help_description.collapse()
} }
}
override fun bind(item: HelpItem) {
item_help_title.setOnClickListener { toggleDescriptionVisibility() }
item_help_toggle_expand.setOnClickListener { toggleDescriptionVisibility() }
item_help_description.text = item.description
item_help_title.text = item.title
} }
} }
} }
class HelpItem(val title: String, val description: String)