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
| interface AdapterDelegate <T> { | |
| fun isForViewType(items: List<T>, position: Int): Boolean | |
| fun onCreateViewHolder(parent: ViewGroup): RecyclerView.ViewHolder | |
| fun onBindViewHolder(items: List<T>, position: Int, holder: RecyclerView.ViewHolder) | |
| } |
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 AdapterDelegateManager<T> { | |
| private val delegates = mutableListOf<AdapterDelegate<T>>() | |
| fun addDelegate(delegate: AdapterDelegate<T>) { | |
| delegates.add(delegate) | |
| } | |
| fun getItemViewType(items: List<T>, position: Int): Int { | |
| delegates.forEachIndexed { index, it -> if (it.isForViewType(items, position)) return index } |
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 GenericAdapter<T>(var items: List<T>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { | |
| private val delegateManager = AdapterDelegateManager<T>() | |
| override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { | |
| return delegateManager.onCreateViewHolder(parent, viewType) | |
| } | |
| override fun getItemCount(): Int { | |
| return items.size |
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 FeedAdapterOld(var items: List<FeedItem> = listOf()) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { | |
| override fun getItemViewType(position: Int): Int { | |
| return when(items[position]) { | |
| is FeedPostItem -> ITEM_POST_ID | |
| is FeedStandardItem -> ITEM_STANDARD_ID | |
| is FeedStoryItem -> ITEM_STORY_ID | |
| else -> -1 | |
| } | |
| } |
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 FeedAdapterOld(var items: List<FeedItem> = listOf()) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { | |
| override fun getItemViewType(position: Int): Int { | |
| return when(items[position]) { | |
| is FeedPostItem -> ITEM_POST_ID | |
| is FeedStandardItem -> ITEM_STANDARD_ID | |
| else -> -1 | |
| } | |
| } |
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 FeedPostAdapterDelegate : AdapterDelegate<FeedItem> { | |
| override fun isForViewType(items: List<FeedItem>, position: Int): Boolean { | |
| return items[position] is FeedPostItem | |
| } | |
| override fun onCreateViewHolder(parent: ViewGroup): RecyclerView.ViewHolder { | |
| return ItemPostViewHolder( | |
| LayoutInflater.from(parent.context).inflate(R.layout.feed_post_item, parent, false) | |
| ) | |
| } |
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 FeedStandardAdapterDelegate : AdapterDelegate<FeedItem> { | |
| override fun isForViewType(items: List<FeedItem>, position: Int): Boolean { | |
| return items[position] is FeedStandardItem | |
| } | |
| override fun onCreateViewHolder(parent: ViewGroup): RecyclerView.ViewHolder { | |
| return ItemStandardViewHolder( | |
| LayoutInflater.from(parent.context).inflate(R.layout.feed_standard_item, parent, false) | |
| ) | |
| } |
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 FeedStoryAdapterDelegate : AdapterDelegate<FeedItem> { | |
| override fun isForViewType(items: List<FeedItem>, position: Int): Boolean { | |
| return items[position] is FeedStoryItem | |
| } | |
| override fun onCreateViewHolder(parent: ViewGroup): RecyclerView.ViewHolder { | |
| return ItemStoryViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.feed_story_item, parent, false)) | |
| } | |
| override fun onBindViewHolder(items: List<FeedItem>, position: Int, holder: RecyclerView.ViewHolder) { |
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
| //If you dont want to use GenericAdapter | |
| class FeedAdapter(var items: List<FeedItem>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { | |
| private val delegateManager = AdapterDelegateManager<FeedItem>() | |
| init { | |
| delegateManager.run { | |
| addDelegate(FeedPostAdapterDelegate()) | |
| addDelegate(FeedStandardAdapterDelegate()) | |
| addDelegate(FeedStoryAdapterDelegate()) |
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 WithoutHolderActivity : AppCompatActivity() { | |
| private val adapter = GenericAdapter(Utils.getFeedItems()) | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_without_holder) | |
| initAdapter() | |
| initRV() | |
| } |