|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Hossain Khan |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.hossainkhan.android.demo.ui.common |
| 18 | + |
| 19 | +import android.view.ViewGroup |
| 20 | +import androidx.databinding.ViewDataBinding |
| 21 | +import androidx.recyclerview.widget.AsyncDifferConfig |
| 22 | +import androidx.recyclerview.widget.DiffUtil |
| 23 | +import androidx.recyclerview.widget.ListAdapter |
| 24 | + |
| 25 | +/** |
| 26 | + * A generic RecyclerView adapter that uses Data Binding & DiffUtil. |
| 27 | + * |
| 28 | + * Here is an example of adapter with diff utils and click listener implemented. |
| 29 | + * ```kotlin |
| 30 | + * class ExampleListAdapter( |
| 31 | + * private val itemClickCallback: ((ModelObject) -> Unit)? |
| 32 | + * ) : DataBoundListAdapter<ModelObject, ListItemBinding>( |
| 33 | + * diffCallback = object : DiffUtil.ItemCallback<ModelObject>() { |
| 34 | + * override fun areItemsTheSame(oldItem: ModelObject, newItem: ModelObject): Boolean { |
| 35 | + * return oldItem.id == newItem.id |
| 36 | + * } |
| 37 | + * |
| 38 | + * override fun areContentsTheSame(oldItem: ModelObject, newItem: ModelObject): Boolean { |
| 39 | + * return oldItem == newItem |
| 40 | + * } |
| 41 | + * } |
| 42 | + * ) { |
| 43 | + * |
| 44 | + * override fun createBinding(parent: ViewGroup): ListItemBinding { |
| 45 | + * val binding = DataBindingUtil.inflate<ListItemBinding>( |
| 46 | + * LayoutInflater.from(parent.context), R.layout.list_item, |
| 47 | + * parent, false) |
| 48 | + * |
| 49 | + * binding.root.setOnClickListener { |
| 50 | + * binding.data?.let { |
| 51 | + * itemClickCallback?.invoke(it) |
| 52 | + * } |
| 53 | + * } |
| 54 | + * return binding |
| 55 | + * } |
| 56 | + * |
| 57 | + * override fun bind(binding: ListItemBinding, item: ModelObject) { |
| 58 | + * binding.idea = item |
| 59 | + * } |
| 60 | + * } |
| 61 | + * ``` |
| 62 | + * |
| 63 | + * And here is how the adapter is instantiated in Fragment/Activity |
| 64 | + * ``` |
| 65 | + * val ideaListAdapter = ExampleListAdapter { modelObject -> |
| 66 | + * doActionOnModelObjectSelected(modelObject) |
| 67 | + * } |
| 68 | + * ``` |
| 69 | + * |
| 70 | + * @param <T> Type of the items in the list |
| 71 | + * @param <V> The type of the ViewDataBinding |
| 72 | + */ |
| 73 | +abstract class DataBoundListAdapter<T, V : ViewDataBinding>( |
| 74 | + diffCallback: DiffUtil.ItemCallback<T> |
| 75 | +) : ListAdapter<T, DataBoundViewHolder<V>>( |
| 76 | + AsyncDifferConfig.Builder<T>(diffCallback).build() |
| 77 | +) { |
| 78 | + /** |
| 79 | + * Provides [ViewDataBinding] for the list item after it's inflated with DataBinding. |
| 80 | + */ |
| 81 | + protected abstract fun createBinding(parent: ViewGroup): V |
| 82 | + |
| 83 | + /** |
| 84 | + * API where view should bound with the data model item. |
| 85 | + */ |
| 86 | + protected abstract fun bind(binding: V, item: T) |
| 87 | + |
| 88 | + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DataBoundViewHolder<V> { |
| 89 | + val binding = createBinding(parent) |
| 90 | + return DataBoundViewHolder(binding) |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + override fun onBindViewHolder(holder: DataBoundViewHolder<V>, position: Int) { |
| 95 | + bind(holder.binding, getItem(position)) |
| 96 | + holder.binding.executePendingBindings() |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | +} |
0 commit comments