|
| 1 | +package com.smarttoolfactory.tutorial3_1transitions.adapter |
| 2 | + |
| 3 | +import android.view.LayoutInflater |
| 4 | +import android.view.View |
| 5 | +import android.view.ViewGroup |
| 6 | +import androidx.databinding.DataBindingUtil |
| 7 | +import androidx.databinding.ViewDataBinding |
| 8 | + |
| 9 | +import androidx.recyclerview.widget.RecyclerView |
| 10 | +import com.smarttoolfactory.tutorial3_1transitions.BR |
| 11 | + |
| 12 | +/** |
| 13 | + * Base Adapter class for creating [RecyclerView.Adapter] |
| 14 | + * |
| 15 | + * Process to create Adapter is listed below: |
| 16 | + * * 1- Inflate layout and create binding object with DataBindingUtil.inflate |
| 17 | + * inside onCreateViewHolder() and create ViewHolder |
| 18 | + * |
| 19 | + * * 2- Get binding object inside constructor of MyViewHolder constructor |
| 20 | + * |
| 21 | + * * 3- Bind items to rows inside onCreateViewHolder() method |
| 22 | + * |
| 23 | + */ |
| 24 | +abstract class BaseAdapter : RecyclerView.Adapter<BaseAdapter.MyViewHolder>() { |
| 25 | + |
| 26 | + private var listener: OnRecyclerViewItemClickListener? = null |
| 27 | + |
| 28 | + inner class MyViewHolder// TODO #2 |
| 29 | + (// each data item is just a string in this case |
| 30 | + private val binding: ViewDataBinding |
| 31 | + ) : RecyclerView.ViewHolder(binding.root), View.OnClickListener { |
| 32 | + |
| 33 | + // TODO #3 |
| 34 | + internal fun bind(obj: Any) { |
| 35 | + binding.setVariable(BR.obj, obj) |
| 36 | + binding.executePendingBindings() |
| 37 | + |
| 38 | + // Set click listener |
| 39 | + itemView.setOnClickListener(this) |
| 40 | + } |
| 41 | + |
| 42 | + override fun onClick(v: View) { |
| 43 | + listener?.run { |
| 44 | + onItemClicked(v, layoutPosition) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + // TODO #1 |
| 51 | + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { |
| 52 | + // create a new view |
| 53 | + val layoutInflater = LayoutInflater.from(parent.context) |
| 54 | + val binding = |
| 55 | + DataBindingUtil.inflate<ViewDataBinding>( |
| 56 | + layoutInflater, |
| 57 | + getLayoutIdForType(viewType), |
| 58 | + parent, |
| 59 | + false |
| 60 | + ) |
| 61 | + // set the view's size, margins, paddings and layout parameters |
| 62 | + return MyViewHolder(binding) |
| 63 | + } |
| 64 | + |
| 65 | + // TODO #3 |
| 66 | + override fun onBindViewHolder(holder: MyViewHolder, position: Int) { |
| 67 | + holder.bind(getDataAtPosition(position)) |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + // TODO #3 |
| 72 | + |
| 73 | + /** |
| 74 | + * Get data in position for RecyclerView row. This method is invoked inside |
| 75 | + * onBindViewHolder() method of RecyclerView |
| 76 | + * |
| 77 | + * @param position indicates the item for the current row |
| 78 | + * @return data for the current row |
| 79 | + */ |
| 80 | + abstract fun getDataAtPosition(position: Int): Any |
| 81 | + |
| 82 | + // TODO #1 |
| 83 | + |
| 84 | + /** |
| 85 | + * Get id of layout from R. This method is invoked from onCreateViewHolder method of Adapter |
| 86 | + * |
| 87 | + * @param viewType id of layout row of RecyclerView |
| 88 | + * @return id of layout |
| 89 | + */ |
| 90 | + abstract fun getLayoutIdForType(viewType: Int): Int |
| 91 | + |
| 92 | + /** |
| 93 | + * RecyclerViewClickListener interface helps user to set a clickListener to the |
| 94 | + * RecyclerView. By setting this listener, any item of Recycler View can respond |
| 95 | + * to any interaction. |
| 96 | + */ |
| 97 | + interface OnRecyclerViewItemClickListener { |
| 98 | + /** |
| 99 | + * This is a callback method that be overridden by the class that implements this |
| 100 | + * interface |
| 101 | + */ |
| 102 | + fun onItemClicked(view: View, position: Int) |
| 103 | + } |
| 104 | + |
| 105 | + fun setOnItemClickListener(listener: OnRecyclerViewItemClickListener) { |
| 106 | + this.listener = listener |
| 107 | + } |
| 108 | +} |
0 commit comments