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 MainActivityAdapter : ListAdapter<Image, RecyclerView.ViewHolder>(MainCallback()) { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder{ | |
val viewModel = MainActivityImageViewModel(parent.context.applicationContext as App) | |
val binding = DataBindingUtil.inflate<ItemImageBinding>(LayoutInflater.from(parent.context), R.layout.item_image, parent, false) | |
binding.viewModel = viewModel | |
return ViewHolder(binding) | |
} | |
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { | |
((holder as ViewHolder).binding as ItemImageBinding).viewModel?.setItem(getItem(position)) |
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 MainCallback : DiffUtil.ItemCallback<Image>() { | |
override fun areItemsTheSame(oldItem: Image, newItem: Image): Boolean { | |
return oldItem.id == newItem.id | |
} | |
override fun areContentsTheSame(oldItem: Image, newItem: Image): Boolean { | |
return oldItem == newItem | |
} | |
} |
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 MainActivityAdapter : ListAdapter<BaseRecyclerEntity, RecyclerView.ViewHolder>(object : DiffUtil.ItemCallback<BaseRecyclerEntity>() { | |
override fun areItemsTheSame(oldItem: BaseRecyclerEntity, newItem: BaseRecyclerEntity): Boolean { | |
return oldItem.id == newItem.id | |
} | |
override fun areContentsTheSame(oldItem: BaseRecyclerEntity, newItem: BaseRecyclerEntity): Boolean { | |
return oldItem == newItem | |
} | |
}) |
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
binding.viewModel = viewModel | |
binding.viewModel?.create() | |
binding.recyclerView.adapter = MainActivityAdapter() |
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
@JvmStatic | |
@BindingAdapter("app:set_list") | |
fun setList(recyclerView: RecyclerView, list: List<BaseRecyclerEntity>?) { | |
val adapter = recyclerView.adapter as MainActivityAdapter | |
list?.let { | |
adapter.submitList(list) | |
} | |
} |
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
@JvmStatic | |
@BindingAdapter("app:set_src") | |
fun setSrc(imageView: ImageView, path: String) { | |
if (path.isNotEmpty()) { | |
Picasso.get().load(path).into(imageView) | |
} | |
} |
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:tools="http://schemas.android.com/tools" | |
xmlns:app="http://schemas.android.com/apk/res-auto"> | |
<data> | |
<variable | |
name="viewModel" | |
type="com.generalmobile.app.recyclermultiitems.ui.main.MainActivityViewModel" /> | |
</data> |
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 MainActivityViewModel(app: Application) : BaseViewModel(app) { | |
val list: ObservableList<BaseRecyclerEntity> = ObservableArrayList() | |
fun create(){ | |
list.addAll(ListProvider().getList()) | |
} | |
} |
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 ListProvider{ | |
fun getList(): ArrayList<BaseRecyclerEntity> { | |
val list = arrayListOf<BaseRecyclerEntity>() | |
list.add(Header(0,Type.HEADER,"Lion")) | |
list.add(Image(1,Type.IMAGE,"https://cdn.images.express.co.uk/img/dynamic/galleries/x701/67639.jpg")) | |
list.add(Image(2,Type.IMAGE,"https://thenypost.files.wordpress.com/2018/02/man-eaten-by-lions.jpg?quality=90&strip=all")) | |
list.add(Image(3,Type.IMAGE,"http://chandigarhbytes.com/wp-content/uploads/2017/07/Federico_Veronesi_2009-01-28-Masai-Mara_4728.jpg")) | |
list.add(Header(4,Type.HEADER,"Tiger")) | |
list.add(Image(5,Type.IMAGE,"http://kids.sandiegozoo.org/sites/default/files/2017-06/animal-hero-tiger_0.jpg")) |
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
object Type { | |
const val HEADER = 0 | |
const val IMAGE = 1 | |
} |
NewerOlder