From 7589290a5ad7a67dc7bac34a466ea2f678f124cc Mon Sep 17 00:00:00 2001 From: Stefan Gsottbauer Date: Fri, 6 Nov 2020 10:35:59 +0100 Subject: [PATCH 1/7] added ability to change LengendAdapter --- .../java/com/faskn/lib/ClickablePieChart.kt | 16 ++++- .../com/faskn/lib/legend/LegendAdapter.kt | 62 +++++++++---------- 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt b/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt index dc0d697..91b60b9 100644 --- a/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt +++ b/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt @@ -62,6 +62,8 @@ class ClickablePieChart @JvmOverloads constructor( private var popupText: String? = null private var showPercentage = false + private var legendAdapter : LegendAdapter = LegendAdapter() + init { initAttributes(attrs) } @@ -288,18 +290,26 @@ class ClickablePieChart @JvmOverloads constructor( showPopup = show } + + fun setLegendAdapter(adapter: LegendAdapter) { + legendAdapter = adapter + } + fun showLegend(rootLayout: ViewGroup) { val recyclerView = RecyclerView(context) val linearLayoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) recyclerView.layoutManager = linearLayoutManager - val adapter = LegendAdapter() - slices?.toMutableList()?.let { adapter.setup(it) } - recyclerView.adapter = adapter + recyclerView.adapter = legendAdapter + slices?.toMutableList()?.let { legendAdapter.setup(it) } recyclerView.overScrollMode = OVER_SCROLL_NEVER rootLayout.addView(recyclerView) invalidateAndRequestLayout() } + + + + private fun invalidateAndRequestLayout() { invalidate() requestLayout() diff --git a/lib/src/main/java/com/faskn/lib/legend/LegendAdapter.kt b/lib/src/main/java/com/faskn/lib/legend/LegendAdapter.kt index 23926fe..a6f4047 100644 --- a/lib/src/main/java/com/faskn/lib/legend/LegendAdapter.kt +++ b/lib/src/main/java/com/faskn/lib/legend/LegendAdapter.kt @@ -10,52 +10,46 @@ import com.faskn.lib.R import com.faskn.lib.Slice import kotlinx.android.synthetic.main.item_legend.view.* -class LegendAdapter : RecyclerView.Adapter() { - - private val items = mutableListOf() +open class LegendAdapter : RecyclerView.Adapter() { + protected val items = mutableListOf() var onItemClickListener: ((Slice?) -> Unit)? = null - override fun onCreateViewHolder( - parent: ViewGroup, - viewType: Int - ): LegendAdapter.ItemViewHolder { - return ItemViewHolder( - LayoutInflater.from(parent.context).inflate( - R.layout.item_legend, - parent, - false - ) + fun setup(items: List) { + this.items.clear() + this.items.addAll(items) + notifyDataSetChanged() + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LegendItemViewHolder { + return LegendItemViewHolder( + LayoutInflater.from(parent.context).inflate(R.layout.item_legend, parent, false) ) } - override fun onBindViewHolder(holder: LegendAdapter.ItemViewHolder, position: Int) { + override fun getItemCount(): Int = items.size + + override fun onBindViewHolder(holder: LegendItemViewHolder, position: Int) { holder.bind(items[position]) + + holder.itemView.setOnClickListener { + onItemClickListener?.invoke(items[position]) + } } +} - override fun getItemCount(): Int = items.size - fun setup(items: List) { - this.items.clear() - this.items.addAll(items) - notifyDataSetChanged() +open class LegendItemViewHolder(view: View) : RecyclerView.ViewHolder(view) { + var boundItem: Slice? = null + open fun bind(slice: Slice) { + boundItem = slice + itemView.imageViewCircleIndicator.imageTintList = + ColorStateList.valueOf(ContextCompat.getColor(itemView.context, slice.color)) + itemView.textViewSliceTitle.text = slice.name } +} + - inner class ItemViewHolder(view: View) : RecyclerView.ViewHolder(view) { - private var boundItem: Slice? = null - init { - itemView.setOnClickListener { - onItemClickListener?.invoke(boundItem) - } - } - fun bind(slice: Slice) { - this.boundItem = slice - itemView.imageViewCircleIndicator.imageTintList = - ColorStateList.valueOf(ContextCompat.getColor(itemView.context, slice.color)) - itemView.textViewSliceTitle.text = slice.name - } - } -} \ No newline at end of file From ed36333278ebed0e5bfc6871d927fcef2ab07d49 Mon Sep 17 00:00:00 2001 From: Stefan Gsottbauer Date: Fri, 6 Nov 2020 12:18:07 +0100 Subject: [PATCH 2/7] Added custom adapter and updated usage --- app/build.gradle | 1 + .../clickablepiechart/CustomLegendAdapter.kt | 31 +++++++++++++++ .../faskn/clickablepiechart/MainActivity.kt | 4 ++ app/src/main/res/drawable/ic_star.xml | 5 +++ app/src/main/res/layout/activity_main.xml | 38 +++++++++++++++++-- .../main/res/layout/custom_item_legend.xml | 28 ++++++++++++++ .../java/com/faskn/lib/ClickablePieChart.kt | 18 +++------ .../com/faskn/lib/legend/LegendAdapter.kt | 1 - 8 files changed, 108 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/com/faskn/clickablepiechart/CustomLegendAdapter.kt create mode 100644 app/src/main/res/drawable/ic_star.xml create mode 100644 app/src/main/res/layout/custom_item_legend.xml diff --git a/app/build.gradle b/app/build.gradle index 8ee8c5d..9361d5f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -35,6 +35,7 @@ dependencies { implementation 'androidx.core:core-ktx:1.3.1' implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + implementation "androidx.recyclerview:recyclerview:1.1.0" testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' diff --git a/app/src/main/java/com/faskn/clickablepiechart/CustomLegendAdapter.kt b/app/src/main/java/com/faskn/clickablepiechart/CustomLegendAdapter.kt new file mode 100644 index 0000000..8cd5585 --- /dev/null +++ b/app/src/main/java/com/faskn/clickablepiechart/CustomLegendAdapter.kt @@ -0,0 +1,31 @@ +package com.faskn.clickablepiechart + +import android.content.res.ColorStateList +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.core.content.ContextCompat +import com.faskn.lib.Slice +import com.faskn.lib.legend.LegendAdapter +import com.faskn.lib.legend.LegendItemViewHolder +import kotlinx.android.synthetic.main.custom_item_legend.view.* + + +class CustomLegendAdapter: LegendAdapter() { + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomLegendItemViewHolder { + return CustomLegendItemViewHolder( + LayoutInflater.from(parent.context).inflate(R.layout.custom_item_legend, parent, false) + ) + } + + // CREATE YOUR OWN ITEM VIEW HOLDER + class CustomLegendItemViewHolder(view: View) : LegendItemViewHolder(view) { + override fun bind(slice: Slice) { + this.boundItem = slice + itemView.imageViewCircleIndicator.imageTintList = + ColorStateList.valueOf(ContextCompat.getColor(itemView.context, slice.color)) + itemView.textViewSliceTitle.text = slice.name + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/faskn/clickablepiechart/MainActivity.kt b/app/src/main/java/com/faskn/clickablepiechart/MainActivity.kt index 18aff92..d2cfc75 100644 --- a/app/src/main/java/com/faskn/clickablepiechart/MainActivity.kt +++ b/app/src/main/java/com/faskn/clickablepiechart/MainActivity.kt @@ -32,6 +32,10 @@ class MainActivity : AppCompatActivity() { chart.setPieChart(pieChart) chart.showLegend(legendLayout) + + //OR SET WITH CUSTOMER LEGEND ADAPTER + chart2.setPieChart(pieChart) + chart2.showLegend(legendLayout2,CustomLegendAdapter()) } private fun provideSlices(): ArrayList { diff --git a/app/src/main/res/drawable/ic_star.xml b/app/src/main/res/drawable/ic_star.xml new file mode 100644 index 0000000..fbdd39f --- /dev/null +++ b/app/src/main/res/drawable/ic_star.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 55f3208..af84670 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,16 +1,18 @@ - - \ No newline at end of file + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/custom_item_legend.xml b/app/src/main/res/layout/custom_item_legend.xml new file mode 100644 index 0000000..0675bb7 --- /dev/null +++ b/app/src/main/res/layout/custom_item_legend.xml @@ -0,0 +1,28 @@ + + + + + + + \ No newline at end of file diff --git a/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt b/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt index 91b60b9..2d26573 100644 --- a/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt +++ b/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt @@ -62,8 +62,6 @@ class ClickablePieChart @JvmOverloads constructor( private var popupText: String? = null private var showPercentage = false - private var legendAdapter : LegendAdapter = LegendAdapter() - init { initAttributes(attrs) } @@ -291,25 +289,19 @@ class ClickablePieChart @JvmOverloads constructor( } - fun setLegendAdapter(adapter: LegendAdapter) { - legendAdapter = adapter - } - - fun showLegend(rootLayout: ViewGroup) { + fun showLegend(rootLayout: ViewGroup, adapter: LegendAdapter = LegendAdapter()) { val recyclerView = RecyclerView(context) - val linearLayoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) + val linearLayoutManager = + LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) recyclerView.layoutManager = linearLayoutManager - recyclerView.adapter = legendAdapter - slices?.toMutableList()?.let { legendAdapter.setup(it) } + recyclerView.adapter = adapter + slices?.toMutableList()?.let { adapter.setup(it) } recyclerView.overScrollMode = OVER_SCROLL_NEVER rootLayout.addView(recyclerView) invalidateAndRequestLayout() } - - - private fun invalidateAndRequestLayout() { invalidate() requestLayout() diff --git a/lib/src/main/java/com/faskn/lib/legend/LegendAdapter.kt b/lib/src/main/java/com/faskn/lib/legend/LegendAdapter.kt index a6f4047..b7208e5 100644 --- a/lib/src/main/java/com/faskn/lib/legend/LegendAdapter.kt +++ b/lib/src/main/java/com/faskn/lib/legend/LegendAdapter.kt @@ -38,7 +38,6 @@ open class LegendAdapter : RecyclerView.Adapter() { } } - open class LegendItemViewHolder(view: View) : RecyclerView.ViewHolder(view) { var boundItem: Slice? = null open fun bind(slice: Slice) { From b2012e9a0897863de1604393521f4556fadd0d6e Mon Sep 17 00:00:00 2001 From: gsotti Date: Fri, 6 Nov 2020 12:20:50 +0100 Subject: [PATCH 3/7] Update README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 9e232ef..2fe733e 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,11 @@ To setup with legend you need an root layout for legend. ```kotlin chart.showLegend(legendLayout) ``` +Or use with custom legend adapter which you can use by inheriting from LegendAdapter +```kotlin +chart.showLegend(legendLayout, CustomLegendAdapter()) +``` + ## XML Attributes From b5040e0812703db75d77d1350231c65e40ae9194 Mon Sep 17 00:00:00 2001 From: gsotti Date: Fri, 6 Nov 2020 12:22:06 +0100 Subject: [PATCH 4/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2fe733e..7104750 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ To setup with legend you need an root layout for legend. ```kotlin chart.showLegend(legendLayout) ``` -Or use with custom legend adapter which you can use by inheriting from LegendAdapter +Or use with custom legend adapter by inheriting from LegendAdapter ```kotlin chart.showLegend(legendLayout, CustomLegendAdapter()) ``` From f8f315cfa4c8fd79fe62b4f89b167307ee89386a Mon Sep 17 00:00:00 2001 From: Stefan Gsottbauer Date: Fri, 6 Nov 2020 12:24:00 +0100 Subject: [PATCH 5/7] removed unused lines --- lib/src/main/java/com/faskn/lib/ClickablePieChart.kt | 2 -- lib/src/main/java/com/faskn/lib/legend/LegendAdapter.kt | 7 +------ 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt b/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt index 2d26573..a86f742 100644 --- a/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt +++ b/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt @@ -288,7 +288,6 @@ class ClickablePieChart @JvmOverloads constructor( showPopup = show } - fun showLegend(rootLayout: ViewGroup, adapter: LegendAdapter = LegendAdapter()) { val recyclerView = RecyclerView(context) val linearLayoutManager = @@ -301,7 +300,6 @@ class ClickablePieChart @JvmOverloads constructor( invalidateAndRequestLayout() } - private fun invalidateAndRequestLayout() { invalidate() requestLayout() diff --git a/lib/src/main/java/com/faskn/lib/legend/LegendAdapter.kt b/lib/src/main/java/com/faskn/lib/legend/LegendAdapter.kt index b7208e5..2323364 100644 --- a/lib/src/main/java/com/faskn/lib/legend/LegendAdapter.kt +++ b/lib/src/main/java/com/faskn/lib/legend/LegendAdapter.kt @@ -46,9 +46,4 @@ open class LegendItemViewHolder(view: View) : RecyclerView.ViewHolder(view) { ColorStateList.valueOf(ContextCompat.getColor(itemView.context, slice.color)) itemView.textViewSliceTitle.text = slice.name } -} - - - - - +} \ No newline at end of file From 87a8520eec6fe39276234a3b5154fa2581271fa9 Mon Sep 17 00:00:00 2001 From: faskN Date: Fri, 6 Nov 2020 14:33:50 +0300 Subject: [PATCH 6/7] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7104750..838ccf0 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ allprojects { Step 2. Add the dependency ```gradle dependencies { - implementation 'com.github.furkanaskin:ClickablePieChart:1.0.7' + implementation 'com.github.furkanaskin:ClickablePieChart:1.0.8' } ``` @@ -51,6 +51,7 @@ Or use with custom legend adapter by inheriting from LegendAdapter ```kotlin chart.showLegend(legendLayout, CustomLegendAdapter()) ``` +Sample Custom Adapter can be found [here](https://github.com/furkanaskin/ClickablePieChart/blob/master/app/src/main/java/com/faskn/clickablepiechart/CustomLegendAdapter.kt) ## XML Attributes
From 25318651f6ebf62144c04fb28e753472d2ccfe0b Mon Sep 17 00:00:00 2001 From: faskN Date: Fri, 6 Nov 2020 14:35:51 +0300 Subject: [PATCH 7/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 838ccf0..f835980 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ To setup with legend you need an root layout for legend. ```kotlin chart.showLegend(legendLayout) ``` -Or use with custom legend adapter by inheriting from LegendAdapter +Or use with custom legend adapter by inheriting from [LegendAdapter](https://github.com/furkanaskin/ClickablePieChart/blob/master/lib/src/main/java/com/faskn/lib/legend/LegendAdapter.kt) ```kotlin chart.showLegend(legendLayout, CustomLegendAdapter()) ```