Skip to content
This repository was archived by the owner on Aug 22, 2024. It is now read-only.

Commit 2d2199e

Browse files
authored
Merge pull request #104 from amardeshbd/102-learning-resources
102 learning resources
2 parents 08041b7 + e78b69d commit 2d2199e

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed

app/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ android {
3434
testOptions {
3535
unitTests.returnDefaultValues = true
3636
}
37+
38+
dataBinding {
39+
// https://developer.android.com/topic/libraries/data-binding/start
40+
enabled = true
41+
}
3742
}
3843

3944
dependencies {
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 androidx.databinding.ViewDataBinding
20+
import androidx.recyclerview.widget.RecyclerView
21+
22+
/**
23+
* A generic ViewHolder that works with a [ViewDataBinding].
24+
* @param <T> The type of the ViewDataBinding.
25+
*/
26+
class DataBoundViewHolder<out T : ViewDataBinding> constructor(
27+
val binding: T
28+
) : RecyclerView.ViewHolder(binding.root)

resources/google-play/Constraint Layout - Google Play Banner.svg

Lines changed: 15 additions & 0 deletions
Loading
Loading

0 commit comments

Comments
 (0)