Created
February 5, 2019 17:43
-
-
Save uziassantosferreira/3d48908fefe3cd485da9fc3822e42c86 to your computer and use it in GitHub Desktop.
Example use mvvm with recyclerview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<layout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools"> | |
<data> | |
<variable | |
name="viewModel" | |
type=".SampleViewModel" /> | |
</data> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recyclerView" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
tools:itemCount="3" | |
tools:listitem="@layout/list_item_posts" | |
app:layoutManager="android.support.v7.widget.LinearLayoutManager" | |
app:data="@{viewModel.posts}"/> | |
</layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface BindableAdapter<T> { | |
fun setData(items: List<T>?) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.youse.shared.binding.adapter | |
import android.animation.Animator | |
import android.animation.AnimatorListenerAdapter | |
import android.databinding.BindingAdapter | |
import android.support.annotation.ColorRes | |
import android.support.annotation.DrawableRes | |
import android.support.design.widget.TextInputLayout | |
import android.support.v4.content.ContextCompat | |
import android.support.v7.widget.RecyclerView | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.ImageView | |
import android.widget.TextView | |
import br.com.youse.shared.R | |
import br.com.youse.shared.presentation.adapter.BindableAdapter | |
object BindingAdapters { | |
@BindingAdapter("data") | |
@JvmStatic | |
@Suppress("UNCHECKED_CAST") | |
fun <T> setRecyclerViewProperties(recyclerView: RecyclerView, items: List<T>?) { | |
if (recyclerView.adapter is BindableAdapter<*>) { | |
(recyclerView.adapter as BindableAdapter<T>).setData(items) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<layout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools"> | |
<data> | |
<variable | |
name="post" | |
type="br.com.sample.model.PostUIModel" /> | |
</data> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:padding="@dimen/base" | |
android:orientation="vertical"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="@{post.title}" /> | |
</LinearLayout> | |
</layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SampleActivity { | |
public override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setupAdapters() | |
} | |
private fun setupAdapters() { | |
val adapter = SimpleAdapter() | |
recyclerViewBeneficiary.adapter = adapter | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SimpleAdapter : RecyclerView.Adapter< | |
SimpleAdapter.BeneficiaryViewHolder>(), | |
BindableAdapter<PostUIModel> { | |
inner class SimpleAdapter(val binding: ListItemPostsBinding) : RecyclerView.ViewHolder(binding.root) | |
override fun setData(items: List<PostUIModel>?) { | |
posts = items ?: emptyList() | |
notifyDataSetChanged() | |
} | |
private var posts = emptyList<PostUIModel>() | |
private var layoutInflater: LayoutInflater? = null | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BeneficiaryViewHolder { | |
if (layoutInflater == null) { | |
layoutInflater = LayoutInflater.from(parent.context) | |
} | |
val binding = DataBindingUtil.inflate<ViewDataBinding>(layoutInflater!!, | |
R.layout.list_item_posts, | |
parent, false) as ListItemPostsBinding | |
return BeneficiaryViewHolder(binding) | |
} | |
override fun getItemCount() = posts.size | |
override fun onBindViewHolder(holder: BeneficiaryViewHolder, position: Int) { | |
holder.binding.post = posts[position] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment