Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.dementiev.realtest
- import android.content.Context
- import android.support.v7.widget.RecyclerView
- import android.view.View
- import android.view.ViewGroup
- import android.widget.TextView
- import android.util.TypedValue
- import android.view.animation.AnimationUtils
- import android.widget.LinearLayout
- import android.widget.FrameLayout
- /**
- * Example imutable adapter with animations
- */
- class TestListAdapter(val context: Context, val items: List<Entity1>, val clickListener: (Entity1) -> Unit) : RecyclerView.Adapter<TestListAdapter.TestViewHolder1>() {
- /* // This is how our entity class looks
- data class Entity1(val _id: Int, val _text: String) {
- var id = _id
- var text = _text
- }
- */
- //optimization of resource usage
- private val DP16: Int = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16f, context.resources.displayMetrics).toInt()
- // do not load the animation here, it wont be beauty, because it is instance of a tweener
- //private val animation: Animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left)
- override fun onBindViewHolder(holder: TestViewHolder1?, position: Int) {
- val item = getItem(position)
- holder?.label?.text = item.text
- holder?.container?.setOnClickListener { clickListener(item) }
- holder?.container?.let { animate(it) } // animate new view
- }
- private fun animate(root: View) {
- AnimationUtils
- .loadAnimation(context, android.R.anim.slide_in_left)
- .let(root::startAnimation)
- }
- override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): TestViewHolder1 {
- val layout = FrameLayout(parent?.context)
- val lp = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
- layout.setPadding(0, DP16, 0, DP16)
- layout.layoutParams = lp
- val textView = TextView(parent?.context)
- val tlp = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
- textView.layoutParams = tlp
- layout.addView(textView)
- return TestViewHolder1(layout, textView)
- }
- override fun getItemCount(): Int {
- return items.size
- }
- private fun getItem(position: Int): Entity1 {
- return items[position]
- }
- class TestViewHolder1(containerView: View?, textView: TextView?) : RecyclerView.ViewHolder(containerView) {
- var container: View? = containerView
- var label: TextView? = textView
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement