Skip to content

Commit

Permalink
Fix stickyHeader flash when call notifyDataSetChange.
Browse files Browse the repository at this point in the history
  • Loading branch information
pokercc committed Nov 22, 2021
1 parent 0ceae96 commit d268555
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pokercc.android.expandablerecyclerview.sample.changeadapter

import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
Expand Down Expand Up @@ -28,7 +29,7 @@ class ChangeAdapterActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.recyclerView.adapter = CountAdapter(3, 2)
binding.recyclerView.adapter = CountAdapter(30, 5)
binding.recyclerView.layoutManager = LinearLayoutManager(this)
binding.changeAdapter.setOnClickListener {
binding.recyclerView.adapter = CountAdapter()
Expand All @@ -51,8 +52,8 @@ private class CountAdapter(
) : ExpandableAdapter<ExpandableAdapter.ViewHolder>() {

fun setNewData() {
groupCount = Random.nextInt(1, 10)
childCount = Random.nextInt(2, 10)
groupCount = Random.nextInt(5, 10)
childCount = Random.nextInt(3, 10)
notifyDataSetChanged()
}

Expand Down Expand Up @@ -84,6 +85,7 @@ private class CountAdapter(
}
}

@SuppressLint("SetTextI18n")
override fun onBindGroupViewHolder(
holder: ExpandableAdapter.ViewHolder,
groupPosition: Int,
Expand All @@ -92,7 +94,7 @@ private class CountAdapter(
) {
if (payloads.isEmpty()) {
(holder as? ParentVH)?.apply {
binding.titleText.text = (groupPosition + 1).toString()
binding.titleText.text = "Group "+(groupPosition + 1).toString()
binding.arrowImage.rotation = if (expand) 0f else -90.0f
}
}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.1"
classpath 'com.android.tools.build:gradle:7.0.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.5'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
Expand Down
2 changes: 1 addition & 1 deletion expandableRecyclerView/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply from: rootProject.file("config.gradle.kt")
apply from: rootProject.file("bintray.gradle")
//apply from: rootProject.file("bintray.gradle")

android {
testOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import androidx.recyclerview.widget.RecyclerView

private const val DEBUG = false

private const val LOG_TAG = "StickyHeader"

open class StickyHeader @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
Expand Down Expand Up @@ -42,6 +44,12 @@ open class StickyHeader @JvmOverloads constructor(
header: View,
y: Float
) {
if (DEBUG) {
Log.d(
LOG_TAG,
"onShowHeader(y:${y},header:${header},this.header:${this.header})"
)
}
if (this.header == header) {
this.header?.y = y
return
Expand All @@ -63,9 +71,6 @@ open class StickyHeader @JvmOverloads constructor(
*/
private class StickyHeaderDecoration(private val onShowHeader: (View, Float) -> Unit) :
RecyclerView.ItemDecoration() {
companion object {
private const val LOG_TAG = "ExpandStickyHeaderD"
}


private var headerGroup = -1
Expand All @@ -88,7 +93,7 @@ private class StickyHeaderDecoration(private val onShowHeader: (View, Float) ->

override fun onChanged() {
super.onChanged()
header = null
// header = null
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip

0 comments on commit d268555

Please sign in to comment.