CymChad / CymChad/BaseRecyclerViewAdapterHelper
getItemCount 的问题
Nobody has claimed this yet.
- Dominant language
- Kotlin
- Stars
- 24.6k
- Forks
- 5.2k
- PR merge metrics
- No merged PRs in 30d
Description
问题,当重写getItemCount 改变item的数量的时候,会造成源码中的getItemViewType 获取错误的item,进而导致崩溃
建议 val dataSize = data.size 改为 val dataSize=getDefItemCount()
/**
* Don't override this method. If need, please override [getDefItemViewType]
* 不要重写此方法,如果有需要,请重写[getDefItemViewType]
*
* @param position Int
* @return Int
*/
override fun getItemViewType(position: Int): Int {
if (hasEmptyView()) {
val header = headerWithEmptyEnable && hasHeaderLayout()
return when (position) {
0 -> if (header) {
HEADER_VIEW
} else {
EMPTY_VIEW
}
1 -> if (header) {
EMPTY_VIEW
} else {
FOOTER_VIEW
}
2 -> FOOTER_VIEW
else -> EMPTY_VIEW
}
}
val hasHeader = hasHeaderLayout()
if (hasHeader && position == 0) {
return HEADER_VIEW
} else {
var adjPosition = if (hasHeader) {
position - 1
} else {
position
}
val dataSize = data.size
return if (adjPosition < dataSize) {
getDefItemViewType(adjPosition)
} else {
adjPosition -= dataSize
val numFooters = if (hasFooterLayout()) {
1
} else {
0
}
if (adjPosition < numFooters) {
FOOTER_VIEW
} else {
LOAD_MORE_VIEW
}
}
}
}
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Search the adapter implementation for the getItemViewType override shown in the issue, then trace how getItemCount and getDefItemCount are used for position calculations. Verify the behavior with headers, footers, and overridden item counts; done means the correct item view type is selected without a crash.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, kotlin
- Domain
- mobile
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100