From 62fed9632e69cb5e46ab3417f63ca0d5632126a2 Mon Sep 17 00:00:00 2001 From: iFanie Date: Mon, 19 Nov 2018 22:45:00 +0200 Subject: [PATCH 1/4] Add reusable item details map & get/add/remove functions --- .../adaptercomponents/AdapterModel.kt | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/accordionrecycler/src/main/java/com/izikode/izilib/accordionrecycler/adaptercomponents/AdapterModel.kt b/accordionrecycler/src/main/java/com/izikode/izilib/accordionrecycler/adaptercomponents/AdapterModel.kt index 1c0cc46..4df4650 100644 --- a/accordionrecycler/src/main/java/com/izikode/izilib/accordionrecycler/adaptercomponents/AdapterModel.kt +++ b/accordionrecycler/src/main/java/com/izikode/izilib/accordionrecycler/adaptercomponents/AdapterModel.kt @@ -1,6 +1,7 @@ package com.izikode.izilib.accordionrecycler.adaptercomponents import com.izikode.izilib.accordionrecycler.AccordionRecyclerData +import com.izikode.izilib.accordionrecycler.AccordionRecyclerItemDetails import com.izikode.izilib.accordionrecycler.AccordionRecyclerPosition import java.lang.ref.WeakReference import java.util.* @@ -23,6 +24,11 @@ class AdapterModel : AdapterContract.Model { */ private val containingListMap: WeakHashMap, MutableList>>> = WeakHashMap() + /** + * Map of reusable item details. + */ + private val detailsMap: WeakHashMap, AccordionRecyclerItemDetails> = WeakHashMap() + override val dataCount: Int get() = dataList.size override fun setData(dataArray: Array>, silently: Boolean) { @@ -300,6 +306,21 @@ class AdapterModel : AdapterContract.Model { } + override fun getDataRecyclingDetails(index: Int): AccordionRecyclerItemDetails = dataList[index].let { wrapper -> + detailsMap[wrapper] ?: + AccordionRecyclerItemDetails( + getDataEnclosedImmediate(index).size, + getDataEnclosedTotal(index).size, + getDataPosition(index), + getDataEnclosedPosition(index), + getEnclosingDataIndex(index)?.let { getDataRecyclingDetails(it) } + ).also { detailsMap[wrapper] = it } + } + + override fun removeDataRecyclingDetails(index: Int) { + detailsMap.remove(dataList[index]) + } + /** * Helper class for the model data items. */ From 2b510af62559543f62d0b1a510d172fcf8fc6b1e Mon Sep 17 00:00:00 2001 From: iFanie Date: Mon, 19 Nov 2018 22:45:27 +0200 Subject: [PATCH 2/4] Update contract & presenter --- .../adaptercomponents/AdapterContract.kt | 15 +++++++++++++++ .../adaptercomponents/AdapterPresenter.kt | 11 ++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/accordionrecycler/src/main/java/com/izikode/izilib/accordionrecycler/adaptercomponents/AdapterContract.kt b/accordionrecycler/src/main/java/com/izikode/izilib/accordionrecycler/adaptercomponents/AdapterContract.kt index 258ca09..b96dca0 100644 --- a/accordionrecycler/src/main/java/com/izikode/izilib/accordionrecycler/adaptercomponents/AdapterContract.kt +++ b/accordionrecycler/src/main/java/com/izikode/izilib/accordionrecycler/adaptercomponents/AdapterContract.kt @@ -119,6 +119,21 @@ interface AdapterContract { */ fun getDataEnclosedPosition(index: Int): AccordionRecyclerPosition + /** + * Builds additional info required by an item being recycled. + * + * @param index The item that is being recycled. + * @return The additional info needed for the item being recycled. + */ + fun getDataRecyclingDetails(index: Int): AccordionRecyclerItemDetails + + /** + * Clears the reusable additional recycling info for the item at the provided index. + * + * @param index The item whose additional info will be removed. + */ + fun removeDataRecyclingDetails(index: Int) + } abstract class View : RecyclerView.Adapter() diff --git a/accordionrecycler/src/main/java/com/izikode/izilib/accordionrecycler/adaptercomponents/AdapterPresenter.kt b/accordionrecycler/src/main/java/com/izikode/izilib/accordionrecycler/adaptercomponents/AdapterPresenter.kt index a7b5037..af5a787 100644 --- a/accordionrecycler/src/main/java/com/izikode/izilib/accordionrecycler/adaptercomponents/AdapterPresenter.kt +++ b/accordionrecycler/src/main/java/com/izikode/izilib/accordionrecycler/adaptercomponents/AdapterPresenter.kt @@ -76,11 +76,7 @@ class AdapterPresenter( override fun getItemEnclosedPosition(position: Int): AccordionRecyclerPosition = model.getDataEnclosedPosition(position) - override fun getItemRecyclingDetails(position: Int): AccordionRecyclerItemDetails = AccordionRecyclerItemDetails( - getItemEnclosedImmediateSum(position), getItemEnclosedTotalSum(position), - getItemPosition(position), getItemEnclosedPosition(position), - model.getEnclosingDataIndex(position)?.let { getItemRecyclingDetails(it) } - ) + override fun getItemRecyclingDetails(position: Int): AccordionRecyclerItemDetails = model.getDataRecyclingDetails(position) override fun onItemSetChanged() { view.notifyDataSetChanged() @@ -104,8 +100,13 @@ class AdapterPresenter( override fun onItemsChanged(startingPosition: Int, numberOfItemsChanged: Int) { if (numberOfItemsChanged == 1) { + model.removeDataRecyclingDetails(startingPosition) view.notifyItemChanged(startingPosition) } else if (numberOfItemsChanged > 1) { + repeat(numberOfItemsChanged) { iteration -> + model.removeDataRecyclingDetails(startingPosition + iteration) + } + view.notifyItemRangeChanged(startingPosition, numberOfItemsChanged) } } From 52144d7ad6460788ce80583950d1e836b6b7ef40 Mon Sep 17 00:00:00 2001 From: iFanie Date: Mon, 19 Nov 2018 22:45:48 +0200 Subject: [PATCH 3/4] Update demo project --- .../accordionrecyclerdemo/MainActivity.kt | 49 ++++++++++++------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/izikode/izilib/accordionrecyclerdemo/MainActivity.kt b/app/src/main/java/com/izikode/izilib/accordionrecyclerdemo/MainActivity.kt index 782b1f2..aa62135 100644 --- a/app/src/main/java/com/izikode/izilib/accordionrecyclerdemo/MainActivity.kt +++ b/app/src/main/java/com/izikode/izilib/accordionrecyclerdemo/MainActivity.kt @@ -9,32 +9,46 @@ import java.util.* class MainActivity : AppCompatActivity() { + var grayIndex = 0 private val colorData = arrayListOf().apply { - add(GrayData("1 gray")) - addAll(redArray(2)) - add(GrayData("2 gray")) - add(GrayData("3 gray")) - addAll(redArray(4)) - add(GrayData("4 gray")) - addAll(redArray(2)) + repeat(100) { + add(gray()) + addAll(redArray(2)) + add(gray()) + add(gray()) + addAll(redArray(8)) + add(gray()) + add(gray()) + add(gray()) + addAll(redArray(3)) + add(gray()) + addAll(redArray(1)) + add(gray()) + addAll(redArray(4)) + } } fun getNewPink() = pinkArray(-1, Random().nextInt(4) + 1) - private fun redArray(sum: Int): Array = Array(sum) { index -> - RedData("$index red").apply { - arrayOfPink = pinkArray(index, Random().nextInt(8) + 1) - } + private fun gray() = GrayData("$grayIndex gray").also { grayIndex += 1 } + + var redIndex = 0 + private fun redArray(sum: Int): Array = Array(sum) { + RedData("$redIndex red").apply { + arrayOfPink = pinkArray(redIndex, Random().nextInt(8) + 1) + }.also { redIndex += 1 } } - private fun pinkArray(forRed: Int, sum: Int): Array = Array(sum) { index -> - PinkData("$index pink of $forRed red").apply { - arrayOfWhite = whiteArray(index, Random().nextInt(9)) - } + var pinkIndex = 0 + private fun pinkArray(forRed: Int, sum: Int): Array = Array(sum) { + PinkData("$pinkIndex pink of $forRed red").apply { + arrayOfWhite = whiteArray(pinkIndex, Random().nextInt(9)) + }.also { pinkIndex += 1 } } - private fun whiteArray(forPink: Int, sum: Int): Array = Array(sum) { index -> - WhiteData("$index white of $forPink pink") + var whiteIndex = 0 + private fun whiteArray(forPink: Int, sum: Int): Array = Array(sum) { + WhiteData("$whiteIndex white of $forPink pink").also { whiteIndex += 1 } } override fun onCreate(savedInstanceState: Bundle?) { @@ -50,6 +64,7 @@ class MainActivity : AppCompatActivity() { } accordionAdapter.colors = colorData + hello.text = "Hello Accordion Recycler! size: ${accordionAdapter.itemCount}" } } From 736ad5ff50dd75b9308b42b8367dd5e3875f5a5f Mon Sep 17 00:00:00 2001 From: iFanie Date: Mon, 19 Nov 2018 22:51:19 +0200 Subject: [PATCH 4/4] Update ReadMe --- README.md | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 730dfa7..9c09357 100644 --- a/README.md +++ b/README.md @@ -21,17 +21,23 @@ implementation 'com.izikode.izilib:accordionrecycler:0.3' ```kotlin abstract class ColorData : AccordionRecyclerData -class RedData(var text: String) : ColorData() { +data class RedData( + + var arrayOfPink: Array = arrayOf() + +) : ColorData(RedViewHolder.VIEW_TYPE) { + + override val mainData: ColorData? + get() = this + + override val enclosedDataArray: Array>? + get() = arrayOfPink - override val viewType: Int = RedViewHolder.VIEW_TYPE - override var mainData: ColorData? = this - override var enclosedDataArray: Array>? = arrayOf() - } -class GrayData( ... ) : ColorData() { ... } -class PinkData( ... ) : ColorData() { ... } -class WhiteData( ... ) : ColorData() { ... } +data class GrayData( ... ) : ColorData() { ... } +data class PinkData( ... ) : ColorData() { ... } +data class WhiteData( ... ) : ColorData() { ... } ``` #### Have your ViewHolders extend the ```AccordionRecyclerViewHolder``` abstract class. - Provide the ViewHolder constructor with the parent ViewGroup and the layout to be inflated. @@ -96,9 +102,8 @@ class MainAccordionAdapter : AccordionRecyclerAdapter