Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example use mvvm with recyclerview
<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>
interface BindableAdapter<T> {
fun setData(items: List<T>?)
}
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)
}
}
}
<?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>
class SampleActivity {
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setupAdapters()
}
private fun setupAdapters() {
val adapter = SimpleAdapter()
recyclerViewBeneficiary.adapter = adapter
}
}
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
You can’t perform that action at this time.