Created
April 7, 2019 22:34
-
-
Save kavan-mevada/727395f0785cb35026ab56f913d7508d to your computer and use it in GitHub Desktop.
RecyclerView with ModelView
This file contains hidden or 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"> | |
<data> | |
<variable name="user" type="elementx.test.musicplayer.User"/> | |
</data> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="@{user.firstName}" | |
/> | |
</LinearLayout> | |
</layout> |
This file contains hidden or 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 ListAdapter(val list: MutableList<User>) : RecyclerView.Adapter<ListAdapter.ViewHolder>() { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder = | |
ViewHolder(inflate(LayoutInflater.from(parent.context), parent, false)) | |
override fun getItemCount() = list.size | |
override fun onBindViewHolder(holder: ViewHolder, position: Int) = holder.bind(position) | |
inner class ViewHolder(private val binding: ListItemTwoTxtImgBinding) : RecyclerView.ViewHolder(binding.root) { | |
fun bind(position: Int) { | |
binding.user = list[position] | |
} | |
} | |
} |
This file contains hidden or 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
val layoutManager = LinearLayoutManager(this) | |
recyclerView.layoutManager = layoutManager | |
val list = mutableListOf(User("Kavan", "Mevada"), User("Damini", "Tripathi")) | |
// specify an adapter (see also next example) | |
val mAdapter = ListAdapter(list) | |
recyclerView.adapter = mAdapter |
This file contains hidden or 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 User(val firstName: String, val lastName: String) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment