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

102 learning resources #104

Merged
merged 2 commits into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ android {
testOptions {
unitTests.returnDefaultValues = true
}

dataBinding {
// https://developer.android.com/topic/libraries/data-binding/start
enabled = true
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (c) 2019 Hossain Khan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hossainkhan.android.demo.ui.common

import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.AsyncDifferConfig
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter

/**
* A generic RecyclerView adapter that uses Data Binding & DiffUtil.
*
* Here is an example of adapter with diff utils and click listener implemented.
* ```kotlin
* class ExampleListAdapter(
* private val itemClickCallback: ((ModelObject) -> Unit)?
* ) : DataBoundListAdapter<ModelObject, ListItemBinding>(
* diffCallback = object : DiffUtil.ItemCallback<ModelObject>() {
* override fun areItemsTheSame(oldItem: ModelObject, newItem: ModelObject): Boolean {
* return oldItem.id == newItem.id
* }
*
* override fun areContentsTheSame(oldItem: ModelObject, newItem: ModelObject): Boolean {
* return oldItem == newItem
* }
* }
* ) {
*
* override fun createBinding(parent: ViewGroup): ListItemBinding {
* val binding = DataBindingUtil.inflate<ListItemBinding>(
* LayoutInflater.from(parent.context), R.layout.list_item,
* parent, false)
*
* binding.root.setOnClickListener {
* binding.data?.let {
* itemClickCallback?.invoke(it)
* }
* }
* return binding
* }
*
* override fun bind(binding: ListItemBinding, item: ModelObject) {
* binding.idea = item
* }
* }
* ```
*
* And here is how the adapter is instantiated in Fragment/Activity
* ```
* val ideaListAdapter = ExampleListAdapter { modelObject ->
* doActionOnModelObjectSelected(modelObject)
* }
* ```
*
* @param <T> Type of the items in the list
* @param <V> The type of the ViewDataBinding
*/
abstract class DataBoundListAdapter<T, V : ViewDataBinding>(
diffCallback: DiffUtil.ItemCallback<T>
) : ListAdapter<T, DataBoundViewHolder<V>>(
AsyncDifferConfig.Builder<T>(diffCallback).build()
) {
/**
* Provides [ViewDataBinding] for the list item after it's inflated with DataBinding.
*/
protected abstract fun createBinding(parent: ViewGroup): V

/**
* API where view should bound with the data model item.
*/
protected abstract fun bind(binding: V, item: T)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DataBoundViewHolder<V> {
val binding = createBinding(parent)
return DataBoundViewHolder(binding)
}


override fun onBindViewHolder(holder: DataBoundViewHolder<V>, position: Int) {
bind(holder.binding, getItem(position))
holder.binding.executePendingBindings()
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2019 Hossain Khan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hossainkhan.android.demo.ui.common

import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView

/**
* A generic ViewHolder that works with a [ViewDataBinding].
* @param <T> The type of the ViewDataBinding.
*/
class DataBoundViewHolder<out T : ViewDataBinding> constructor(
val binding: T
) : RecyclerView.ViewHolder(binding.root)
15 changes: 15 additions & 0 deletions resources/google-play/Constraint Layout - Google Play Banner.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/google-play/ConstraintLayout-banner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.