diff --git a/README.md b/README.md index cfe87aa..7c0c75f 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ RecyclerView adapter with nested items & expand/contract functionality ## Installation ``` -implementation 'com.izikode.izilib:accordionrecycler:0.1' +implementation 'com.izikode.izilib:accordionrecycler:0.3' ``` ## Usage #### Have your Model Classes implement the ```AccordionRecyclerData``` interface. diff --git a/accordionrecycler/src/main/java/com/izikode/izilib/accordionrecycler/AccordionRecyclerAdapter.kt b/accordionrecycler/src/main/java/com/izikode/izilib/accordionrecycler/AccordionRecyclerAdapter.kt index 7d78488..de39f82 100644 --- a/accordionrecycler/src/main/java/com/izikode/izilib/accordionrecycler/AccordionRecyclerAdapter.kt +++ b/accordionrecycler/src/main/java/com/izikode/izilib/accordionrecycler/AccordionRecyclerAdapter.kt @@ -131,15 +131,7 @@ abstract class AccordionRecyclerAdapter : AdapterContract. override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder = buildViewHolder(p0, p1) override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) { - updateViewHolder( - position, - viewHolder, - presenter.getItem(position), - presenter.getItemEnclosedImmediateSum(position), - presenter.getItemEnclosedTotalSum(position), - presenter.getItemPosition(position), - presenter.getItemEnclosedPosition(position) - ) + updateViewHolder(position, viewHolder, presenter.getItem(position), presenter.getItemRecyclingDetails(position)) } } \ No newline at end of file diff --git a/accordionrecycler/src/main/java/com/izikode/izilib/accordionrecycler/AccordionRecyclerItemDetails.kt b/accordionrecycler/src/main/java/com/izikode/izilib/accordionrecycler/AccordionRecyclerItemDetails.kt new file mode 100644 index 0000000..816c8ec --- /dev/null +++ b/accordionrecycler/src/main/java/com/izikode/izilib/accordionrecycler/AccordionRecyclerItemDetails.kt @@ -0,0 +1,33 @@ +package com.izikode.izilib.accordionrecycler + +/** + * Data class containing additional information about an item being recycled. + */ +data class AccordionRecyclerItemDetails( + + /** + * The number of immediate children of the current item. + */ + val numberOfImmediateEnclosedItems: Int, + + /** + * The number of total children, immediate or not, of the current item. + */ + val numberOfTotalEnclosedItems: Int, + + /** + * The overall position info of the current item. + */ + val overallPosition: AccordionRecyclerPosition, + + /** + * The position info of the current item, relative to it's parent item. + */ + val enclosedPosition: AccordionRecyclerPosition, + + /** + * The recycling details of the item enclosing the current, if existing. + */ + val enclosingDetails: AccordionRecyclerItemDetails? + +) \ No newline at end of file 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 7b22833..258ca09 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 @@ -3,6 +3,7 @@ package com.izikode.izilib.accordionrecycler.adaptercomponents import android.support.v7.widget.RecyclerView import android.view.ViewGroup import com.izikode.izilib.accordionrecycler.AccordionRecyclerData +import com.izikode.izilib.accordionrecycler.AccordionRecyclerItemDetails import com.izikode.izilib.accordionrecycler.AccordionRecyclerPosition import com.izikode.izilib.accordionrecycler.AccordionRecyclerViewHolder import java.util.* @@ -71,6 +72,13 @@ interface AdapterContract { */ fun getData(index: Int): DataType? + /** + * Gets the index of the data enclosing the data at the provided index. + * @param enclosedIndex The index of the enclosed data. + * @return The index of the enclosing index, if existing. + */ + fun getEnclosingDataIndex(enclosedIndex: Int): Int? + /** * Gets the viewType of the data stored at the provided index. * @@ -140,12 +148,9 @@ interface AdapterContract { * @param position The position of the data on the recycler. * @param viewHolder The ViewHolder to be renewed. * @param data The data to be used for the renewal of the ViewHolder. - * @param immediateEnclosedItemsSum The number of immediate children of the current item. - * @param totalEnclosedItemsSum The number of total children, immediate or not, of the current item. - * @param overallPosition The overall position info of the current item. - * @param enclosedPosition The position info of the current item, relative to it's parent item. + * @param details Details about the item being recycled. */ - abstract fun updateViewHolder(position: Int, viewHolder: ViewHolder, data: DataType?, immediateEnclosedItemsSum: Int, totalEnclosedItemsSum: Int, overallPosition: AccordionRecyclerPosition, enclosedPosition: AccordionRecyclerPosition) + abstract fun updateViewHolder(position: Int, viewHolder: ViewHolder, data: DataType?, details: AccordionRecyclerItemDetails) } @@ -295,6 +300,13 @@ interface AdapterContract { */ fun getItemEnclosedPosition(position: Int): AccordionRecyclerPosition + /** + * Builds additional info required by an item being recycled. + * @param position The item that is being recycled. + * @return The additional info needed for the item being recycled. + */ + fun getItemRecyclingDetails(position: Int): AccordionRecyclerItemDetails + /** * Function called when the data set of the adapter changes. */ 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 742c176..1c0cc46 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 @@ -206,6 +206,8 @@ class AdapterModel : AdapterContract.Model { override fun getData(index: Int): DataType? = dataList[index].data + override fun getEnclosingDataIndex(enclosedIndex: Int): Int? = dataList[enclosedIndex].enclosingWrapper?.let { dataList.indexOf(it) } + override fun getDataViewType(index: Int): Int = dataList[index].viewType override fun getDataEnclosedImmediate(index: Int): SortedMap = containingListMap[dataList[index]]?.let { 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 e4d93ce..a7b5037 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 @@ -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 com.izikode.izilib.accordionrecycler.AccordionRecyclerViewHolder @@ -75,6 +76,12 @@ 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 onItemSetChanged() { view.notifyDataSetChanged() } diff --git a/app/src/main/java/com/izikode/izilib/accordionrecyclerdemo/MainAccordionAdapter.kt b/app/src/main/java/com/izikode/izilib/accordionrecyclerdemo/MainAccordionAdapter.kt index 12d4093..c4055a5 100644 --- a/app/src/main/java/com/izikode/izilib/accordionrecyclerdemo/MainAccordionAdapter.kt +++ b/app/src/main/java/com/izikode/izilib/accordionrecyclerdemo/MainAccordionAdapter.kt @@ -3,7 +3,7 @@ package com.izikode.izilib.accordionrecyclerdemo import android.view.ViewGroup import com.izikode.izilib.accordionrecycler.AccordionRecyclerAdapter import com.izikode.izilib.accordionrecycler.AccordionRecyclerData -import com.izikode.izilib.accordionrecycler.AccordionRecyclerPosition +import com.izikode.izilib.accordionrecycler.AccordionRecyclerItemDetails import com.izikode.izilib.accordionrecyclerdemo.data.* import com.izikode.izilib.accordionrecyclerdemo.viewholder.* @@ -47,11 +47,11 @@ class MainAccordionAdapter : AccordionRecyclerAdapter, data: ColorData?, immediateEnclosedItemsSum: Int, totalEnclosedItemsSum: Int, overallPosition: AccordionRecyclerPosition, enclosedPosition: AccordionRecyclerPosition) { + override fun updateViewHolder(position: Int, viewHolder: ColorViewHolder, data: ColorData?, details: AccordionRecyclerItemDetails) { when (viewHolder) { is RedViewHolder -> viewHolder.apply { - update(data as RedData?, overallPosition, immediateEnclosedItemsSum) + update(data as RedData?, details.overallPosition, details.numberOfImmediateEnclosedItems) onClick = { position, enclosedSum -> if (enclosedSum > 0) { @@ -63,7 +63,7 @@ class MainAccordionAdapter : AccordionRecyclerAdapter viewHolder.apply { - update(data as PinkData?, overallPosition, enclosedPosition, totalEnclosedItemsSum) + update(data as PinkData?, details.overallPosition, details.enclosedPosition, details.numberOfTotalEnclosedItems) onClick = { position, enclosedSum -> if (enclosedSum > 0) { @@ -75,18 +75,18 @@ class MainAccordionAdapter : AccordionRecyclerAdapter viewHolder.apply { - update(data as EmptyPinkViewHolder.EmptyPinkData?, overallPosition, enclosedPosition, totalEnclosedItemsSum) + update(data as EmptyPinkViewHolder.EmptyPinkData?, details.overallPosition, details.enclosedPosition, details.numberOfTotalEnclosedItems) } is WhiteViewHolder -> viewHolder.apply { - update(data as WhiteData?, overallPosition, enclosedPosition) + update(data as WhiteData?, details.overallPosition, details.enclosedPosition) onClick = { removeItem(it) } } - else -> (viewHolder as GrayViewHolder).update(data as GrayData?, overallPosition) + else -> (viewHolder as GrayViewHolder).update(data as GrayData?, details.overallPosition) } } diff --git a/build.gradle b/build.gradle index 769bf90..737da9a 100644 --- a/build.gradle +++ b/build.gradle @@ -36,9 +36,9 @@ buildscript { ], Build : [ - CODE : 2, + CODE : 3, MAJOR : 0, - MINOR : 2 + MINOR : 3 ] ] }