Skip to content

Commit

Permalink
Fix: fixed the issue when add footer and header.
Browse files Browse the repository at this point in the history
  • Loading branch information
pokk committed Feb 19, 2019
1 parent b8d34e7 commit e7c344a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,42 @@ abstract class AdaptiveAdapter<VT : ViewTypeFactory, M : IVisitable<VT>, VH : Re
var headerEntity: M? = null
set(value) {
value?.let {
field = it
dataList.add(0, it)
notifyItemInserted(0)
if (field == null) {
dataList.add(0, it)
notifyItemInserted(0)
}
else {
dataList.removeAt(0)
dataList.add(0, it)
notifyItemChanged(0)
}
} ?: run {
dataList.removeAt(0)
notifyItemRemoved(0)
if (field != null) {
dataList.removeAt(0)
notifyItemRemoved(0)
}
}
field = value
}
var footerEntity: M? = null
set(value) {
value?.let {
field = it
dataList.add(dataList.size, it)
notifyItemChanged(dataList.size)
if (field == null) {
dataList.add(dataList.size, it)
notifyItemInserted(dataList.size)
}
else {
dataList.removeAt(dataList.size - 1)
dataList.add(dataList.size, it)
notifyItemChanged(dataList.size - 1)
}
} ?: run {
dataList.removeAt(dataList.size)
notifyItemChanged(dataList.size)
if (field != null) {
dataList.removeAt(dataList.size - 1)
notifyItemRemoved(dataList.size)
}
}
field = value
}
open var diffUtil: AdaptiveDiffUtil<VT, M> = MultiDiffUtil()
open var useDiffUtilUpdate = true
Expand Down Expand Up @@ -76,16 +94,22 @@ abstract class AdaptiveAdapter<VT : ViewTypeFactory, M : IVisitable<VT>, VH : Re

// OPTIMIZE(jieyi): 2018/12/04 There's no checking bounding.
open fun appendList(list: MutableList<M>) {
val startIndex = dataList.size
var startIndex = dataList.size
if (footerEntity != null)
startIndex--
// [toMutableList()] will create a new [ArrayList].
val newList = dataList.toMutableList().apply { addAll(startIndex, list) }
updateList { newList }
}

open fun append(item: M) {
val newList = dataList.toMutableList().apply { add(item) }
val newList = dataList.toMutableList().apply {
if (footerEntity != null) add(dataList.size - 1, item) else add(item)
}
updateList { newList }
}

@Deprecated("There's some bugs with index of header and footer")
open fun add(position: Int, item: M) {
var size = dataList.size
if (headerEntity != null) size--
Expand All @@ -94,18 +118,20 @@ abstract class AdaptiveAdapter<VT : ViewTypeFactory, M : IVisitable<VT>, VH : Re
if (size <= 0) throw IndexOutOfBoundsException()

val newList = dataList.toMutableList().apply {
add(position + (if (headerEntity == null) 0 else 1), item)
add(position + (if (headerEntity == null) 0 else 1) + (if (footerEntity == null) 0 else -1), item)
}
updateList { newList }
}

@Deprecated("There's some bugs with index of header and footer")
open fun add(position: Int, list: MutableList<M>) {
val newList = dataList.toMutableList().apply {
addAll(position + (if (headerEntity == null) 0 else 1), list)
addAll(position + (if (headerEntity == null) 0 else 1) + (if (footerEntity == null) 0 else -1), list)
}
updateList { newList }
}

@Deprecated("There's some bugs with index of header and footer")
open fun dropList(startIndex: Int, endIndex: Int) {
when {
startIndex < 0 || endIndex >= dataList.size -> throw IndexOutOfBoundsException("The range is over than list.")
Expand All @@ -117,7 +143,10 @@ abstract class AdaptiveAdapter<VT : ViewTypeFactory, M : IVisitable<VT>, VH : Re
updateList { newList }
}

open fun dropAt(index: Int) = dropList(index, index)
@Deprecated("There's some bugs with index of header and footer")
open fun dropAt(index: Int) {
dropList(index, index)
}

open fun clearList(header: Boolean = true, footer: Boolean = true): Boolean {
var from = 0
Expand All @@ -129,6 +158,8 @@ abstract class AdaptiveAdapter<VT : ViewTypeFactory, M : IVisitable<VT>, VH : Re
from++
if (!footer && headerEntity != null)
to--
if (header) headerEntity = null
if (footer) footerEntity = null

dropList(from, to)

Expand All @@ -137,6 +168,8 @@ abstract class AdaptiveAdapter<VT : ViewTypeFactory, M : IVisitable<VT>, VH : Re

open fun replaceWholeList(newList: MutableList<M>) {
updateList { newList }
headerEntity = null
footerEntity = null
}

private fun updateList(getNewListBlock: () -> MutableList<M>) {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.android.tools.build:gradle:3.3.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
9 changes: 6 additions & 3 deletions sample/src/main/java/com/devrapid/example/ExpandAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@ import com.devrapid.example.viewtype.MultiTypeFactory
* @author jieyi
* @since 9/7/17
*/
class ExpandAdapter(override var dataList: MutableList<IExpandVisitor>):
class ExpandAdapter(override var dataList: MutableList<IExpandVisitor>) :
AdaptiveAdapter<MultiTypeFactory, IExpandVisitor, AdaptiveViewHolder<MultiTypeFactory, IExpandVisitor>>() {
override var typeFactory: MultiTypeFactory = MultiTypeFactory()
// override var useDiffUtilUpdate = false
private val originalParentPosition: MutableList<Int> = MutableList(this.dataList.size) { 0 }

class ExpandDiffUtil(private var oldList: MutableList<IExpandVisitor>,
private var newList: MutableList<IExpandVisitor>): DiffUtil.Callback() {
class ExpandDiffUtil(
private var oldList: MutableList<IExpandVisitor>,
private var newList: MutableList<IExpandVisitor>
) : DiffUtil.Callback() {
override fun getOldListSize(): Int = this.oldList.size

override fun getNewListSize(): Int = newList.size

override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean =
this.oldList[oldItemPosition].hashCode() == this.newList[newItemPosition].hashCode()
// (this.oldList[oldItemPosition] as Person).name == (this.newList[newItemPosition] as Person).name

override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean = true
}
Expand Down
12 changes: 5 additions & 7 deletions sample/src/main/java/com/devrapid/example/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@ class MainActivity : AppCompatActivity() {
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
recyclerView.adapter = adapter

adapter.headerEntity = Person("Google @@@@@@@@")
adapter.footerEntity = Person("Google !!!!!!!!")

btn_add.setOnClickListener {
adapter.appendList(listOf(Person("Google $a")) as MutableList<IExpandVisitor>)
a++
adapter.clearList(false, false)
}
btn_minus.setOnClickListener {
adapter.dropList(1, 1)
}
btn_minus.setOnLongClickListener {
adapter.clearList()
true
adapter.append(Person("BBBBBBBB ${a++}"))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.devrapid.example.viewholder

import android.view.View
import com.devrapid.adaptiverecyclerview.AdaptiveAdapter
import com.devrapid.adaptiverecyclerview.AdaptiveViewHolder
import com.devrapid.example.ExpandAdapter
import com.devrapid.example.model.Person
Expand All @@ -13,7 +14,7 @@ import kotlinx.android.synthetic.main.item_first.view.tv_title
* @since 9/6/17
*/
class FirstViewHolder(view: View): AdaptiveViewHolder<MultiTypeFactory, Person>(view) {
override fun initView(model: Person, position: Int, adapter: Any) {
override fun initView(model: Person, position: Int, adapter: AdaptiveAdapter<*, *, *>) {
adapter as ExpandAdapter
this.itemView.tv_title.text = model.name
this.itemView.btn.setOnClickListener {
Expand All @@ -26,4 +27,4 @@ class FirstViewHolder(view: View): AdaptiveViewHolder<MultiTypeFactory, Person>(
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.devrapid.example.viewholder

import android.view.View
import com.devrapid.adaptiverecyclerview.AdaptiveAdapter
import com.devrapid.adaptiverecyclerview.AdaptiveViewHolder
import com.devrapid.example.ExpandAdapter
import com.devrapid.example.model.Animal
Expand All @@ -12,8 +13,8 @@ import kotlinx.android.synthetic.main.item_second.view.tv_subtitle
* @since 9/6/17
*/
class SecondViewHolder(view: View): AdaptiveViewHolder<MultiTypeFactory, Animal>(view) {
override fun initView(model: Animal, position: Int, adapter: Any) {
override fun initView(model: Animal, position: Int, adapter: AdaptiveAdapter<*, *, *>) {
adapter as ExpandAdapter
this.itemView.tv_subtitle.text = model.name
}
}
}

0 comments on commit e7c344a

Please sign in to comment.