Created
May 20, 2018 17:53
-
-
Save vorobeij/e66dba7b9c3ec821248242b8832ab435 to your computer and use it in GitHub Desktop.
Recycler view adapter
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 GalleryAdapter(var items:List<DataHolder>) : Adapter<GalleryAdapter.ViewHolder>() { | |
| override fun onBindViewHolder(holder: ViewHolder, | |
| position: Int) { | |
| TODO("not implemented") // File | Settings | File Templates. | |
| } | |
| override fun getItemCount(): Int = items.size | |
| override fun onCreateViewHolder(parent: ViewGroup, | |
| viewType: Int): ViewHolder { | |
| val inflater = LayoutInflater.from(parent.context) | |
| val view = inflater.inflate(layout.w_icon_label_checkbox, parent, false) | |
| return ViewHolder(view) | |
| } | |
| class ViewHolder(v: View) : RecyclerView.ViewHolder(v) { | |
| var icon: ImageView? = v.v_icon | |
| } | |
| fun updateAppsList(list: List<DataHolder>) { | |
| Timber.v("jsp updating apps list. New size: ${list.size}") | |
| val diffRes = DiffUtil.calculateDiff(DiffDataCallback(items, list)) | |
| items = list | |
| diffRes.dispatchUpdatesTo(this) | |
| } | |
| } |
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 DataHolder(var data:String) |
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 DiffDataCallback(var itemsOld: List<DataHolder>, | |
| var itemsNew: List<DataHolder> | |
| ) : Callback() { | |
| override fun areItemsTheSame(oldItemPosition: Int, | |
| newItemPosition: Int): Boolean { | |
| val itemOld = itemsOld[oldItemPosition] | |
| val itemNew = itemsNew[newItemPosition] | |
| return itemNew.data == itemOld.data | |
| } | |
| override fun areContentsTheSame(oldItemPosition: Int, | |
| newItemPosition: Int): Boolean { | |
| return true | |
| } | |
| override fun getOldListSize(): Int = itemsOld.size | |
| override fun getNewListSize(): Int = itemsNew.size | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment