diff --git a/README.md b/README.md
index 9e232ef..f835980 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'
}
```
@@ -47,6 +47,12 @@ 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](https://github.com/furkanaskin/ClickablePieChart/blob/master/lib/src/main/java/com/faskn/lib/legend/LegendAdapter.kt)
+```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
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 7e4926e..43fbb67 100644
--- a/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt
+++ b/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt
@@ -297,13 +297,13 @@ class ClickablePieChart @JvmOverloads constructor(
popupWindow?.dismiss()
}
- 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
- val adapter = LegendAdapter()
- slices?.toMutableList()?.let { adapter.setup(it) }
recyclerView.adapter = adapter
+ slices?.toMutableList()?.let { adapter.setup(it) }
recyclerView.overScrollMode = OVER_SCROLL_NEVER
rootLayout.addView(recyclerView)
invalidateAndRequestLayout()
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..2323364 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,40 @@ 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
- )
- )
- }
-
- override fun onBindViewHolder(holder: LegendAdapter.ItemViewHolder, position: Int) {
- holder.bind(items[position])
- }
-
- override fun getItemCount(): Int = items.size
-
fun setup(items: List) {
this.items.clear()
this.items.addAll(items)
notifyDataSetChanged()
}
- inner class ItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
+ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LegendItemViewHolder {
+ return LegendItemViewHolder(
+ LayoutInflater.from(parent.context).inflate(R.layout.item_legend, parent, false)
+ )
+ }
- private var boundItem: Slice? = null
+ override fun getItemCount(): Int = items.size
- init {
- itemView.setOnClickListener {
- onItemClickListener?.invoke(boundItem)
- }
- }
+ override fun onBindViewHolder(holder: LegendItemViewHolder, position: Int) {
+ holder.bind(items[position])
- fun bind(slice: Slice) {
- this.boundItem = slice
- itemView.imageViewCircleIndicator.imageTintList =
- ColorStateList.valueOf(ContextCompat.getColor(itemView.context, slice.color))
- itemView.textViewSliceTitle.text = slice.name
+ holder.itemView.setOnClickListener {
+ onItemClickListener?.invoke(items[position])
}
}
+}
+
+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
+ }
}
\ No newline at end of file