luizgrp / SectionedRecyclerViewAdapter

An Adapter that allows a RecyclerView to be split into Sections with headers and/or footers. Each Section can have its state controlled individually.
MIT License
1.68k stars 372 forks source link

Optimize onCreateViewHolder #201

Closed yccheok closed 3 years ago

yccheok commented 4 years ago

By referring to code at

https://github.com/lisawray/groupie/blob/c3411d814f18e9b308e3b829b1e8a532f34b6ee2/library/src/main/java/com/xwray/groupie/GroupAdapter.java#L526

I believe we may eliminate for loop execution most of the time, in onCreateViewHolder.

The key idea is that, getItemViewType will always be executed before onCreateViewHolder, as RecyclerView need to pass view type information to onCreateViewHolder's function.

My proposed method is as follow.

1) In getItemViewType, cache the last hit Section's TAG.

2) In onCreateViewHolder, get viewType from last hit Section's TAG. Compare with function parameter viewType. If matched, we can proceed with get...ViewHolder(parent, section). The section will retrieved from last hit Section's TAG in step 1.

3) If not matched, we will continue execute with old loop searching code. (I believe most of the time, the hit will success)

Thank you.