Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi selection listview #10

Merged
merged 8 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<com.ss.smartfilterlib.multiselection.MultiSelectionListView
android:id="@+id/multiSelectionListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
app:ss_listitem="@array/array_android_version"/>

</LinearLayout>

Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package com.ss.smartfilterlib

import com.ss.smartfilterlib.data.MultiSelectionParams
import com.ss.smartfilterlib.data.SingleChipSelectionParams
import com.ss.smartfilterlib.multiselection.chipgroup.MultiselectionChipGroup
import com.ss.smartfilterlib.singalchoice.chipgroup.SingleChipGroup
import com.ss.smartfilterlib.singlechoice.radiogroup.MultiLineRadioGroup
import com.ss.smartfilterlib.singlechoice.radiogroup.RowItemRadioGroup
import com.ss.smartfilterlib.singlechoice.radiogroup.SingleLineRadioGroup
import com.ss.smartfilterlib.multiselection.MultiselectionChipGroup
import com.ss.smartfilterlib.singleselection.SingleSelectionChipGroup
import com.ss.smartfilterlib.singleselection.SingleSelectionMultiLineRadioButton
import com.ss.smartfilterlib.singleselection.SingleSelectionItemRadioGroup
import com.ss.smartfilterlib.singleselection.SingleSelectionRadioGroup
import com.ss.smartfilterlib.utils.Params


object SmartFilter {
fun addRadioGroupSingleSelection(singleSelectionParams: () -> Params.SingleSelection) {
val param = singleSelectionParams().data
val singleLineRadioGroup = SingleLineRadioGroup(param.rootView.context)
val singleLineRadioGroup = SingleSelectionRadioGroup(param.rootView.context)
singleLineRadioGroup.configureRadioButton(
param.mData,
param.orientation,
Expand All @@ -26,7 +26,7 @@ object SmartFilter {
}
fun addRadioMultiRawSingleSelection(singleSelectionMultiRawParams: () -> Params.SingleSelectionMultiRaw) {
val param = singleSelectionMultiRawParams().data
val multiLineRadioGroup = MultiLineRadioGroup(param.rootView.context)
val multiLineRadioGroup = SingleSelectionMultiLineRadioButton(param.rootView.context)
multiLineRadioGroup.configureRadioButton(
param.mData,
param.bgSelector,
Expand All @@ -39,7 +39,7 @@ object SmartFilter {

fun addRadioRawItemSingleSelection(singleSelectionParams: () -> Params.SingleSelection) {
val param = singleSelectionParams().data
val rowItemRadioGroup = RowItemRadioGroup(param.rootView.context)
val rowItemRadioGroup = SingleSelectionItemRadioGroup(param.rootView.context)
rowItemRadioGroup.configureRadioButton(
param.mData,
param.orientation,
Expand All @@ -53,7 +53,7 @@ object SmartFilter {

fun addChipGroupSingleSelection(singleChipSelectionParams: () -> SingleChipSelectionParams) {
val param = singleChipSelectionParams()
val singleChipgroup = SingleChipGroup(param.rootView.context)
val singleChipgroup = SingleSelectionChipGroup(param.rootView.context)
singleChipgroup.setData(
param.mData,
param.chipType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ fun interface RadioGroupCallback{
fun interface ChipGroupCallback{
fun onMultiChipCheckedChanged( checkedChipIds: List<Int>)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package com.ss.smartfilterlib.multiselection

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckedTextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.card.MaterialCardView
import com.ss.smartfilterlib.utils.BaseClass
import com.ss.smartfilterlib.R
import com.ss.smartfilterlib.singleselection.SmartOrientation
import com.ss.smartfilterlib.data.RadioGroupData



/**
* created by Mala Ruparel ON 02/05/24
*/
class MultiSelectionListView @JvmOverloads constructor( context: Context,attrs: AttributeSet? = null,defStyle: Int = 0) : BaseClass<RadioGroupData>(context, attrs, defStyle){


init {
initAttributes(attrs=attrs)
initializeView()
populateDataFromAttributes()
}

private fun populateDataFromAttributes() {
val mData = resources.getStringArray(dataFromXml);
val data = mData.map { RadioGroupData(name = it) } as ArrayList<RadioGroupData>
configureView(
data = data,
orientation = orientation,
checkSelector = checkSelector,
primaryTextColor = primaryTextColor,
onCheckedChangeListener = onMultiSelectionClicked
)

}

override fun initializeView() {
layoutManager = when (orientation) {
SmartOrientation.VERTICAL -> LinearLayoutManager(context, VERTICAL, false)
else -> LinearLayoutManager(context, HORIZONTAL, false)
}
}

override fun initAttributes(attrs: AttributeSet?) {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.SingleSelectionView)
try {
primaryTextColor = typedArray.getColor(R.styleable.SingleSelectionView_ss_textselector, primaryTextColor)
orientation = typedArray.getInt(R.styleable.SingleSelectionView_ss_orientation,RecyclerView.VERTICAL)
checkSelector = typedArray.getResourceId(
R.styleable.SingleSelectionView_ss_checkdrawableselector,
R.drawable.ic_check_selector
)
dataFromXml = typedArray.getResourceId(R.styleable.SingleSelectionView_ss_listitem, 0)
} finally {
typedArray.recycle()
}
}

private fun configureView(data: ArrayList<RadioGroupData>, orientation: Int, checkSelector: Int, primaryTextColor: Int, onCheckedChangeListener: ((List<RadioGroupData>) -> Unit)?) {

updateValue(orientation, checkSelector, primaryTextColor, onCheckedChangeListener)
initializeView()
setItems(data)
}

private fun updateValue(orientation: Int,checkSelector: Int, primaryTextColor: Int,onCheckedChangeListener: ((List<RadioGroupData>) -> Unit)?) {
this.onMultiSelectionClicked = onCheckedChangeListener
this.orientation = orientation
this.checkSelector = checkSelector
this.primaryTextColor = primaryTextColor
}
private fun setItems(items: List<RadioGroupData>) {
adapter = MultiSelectionListAdapter().apply { data = items }
}

private inner class MultiSelectionListAdapter() : RecyclerView.Adapter<MultiSelectionListAdapter.MultiselectionViewHolder>() {

private var selectedItemPosition: Int = RecyclerView.NO_POSITION

var data: List<RadioGroupData> = emptyList()
set(value) {
field = value
notifyDataSetChanged()
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MultiselectionViewHolder {
val view = LayoutInflater.from(parent.context) .inflate(R.layout.item_simple_list_checkable, parent, false)
return MultiselectionViewHolder(view)
}

override fun onBindViewHolder(holder: MultiselectionViewHolder, position: Int) {
holder.bind(data[position],position == selectedItemPosition)
}

override fun getItemCount(): Int = data.size

inner class MultiselectionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

val cardView: MaterialCardView by lazy { MaterialCardView(itemView.context) }
val textView: CheckedTextView by lazy { CheckedTextView(itemView.context) }


init {
with(cardView) {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
addView(textView)
}

(itemView as ViewGroup).addView(cardView)

itemView.setOnClickListener {
val position = adapterPosition
if (position != RecyclerView.NO_POSITION) {
toggleItemSelection(position)
}
}
}


fun bind(data: RadioGroupData, isSelected: Boolean) {
textView.apply {
isChecked = selectedItemsPositions.contains(adapterPosition)
applyTextAttributes(this)
applyPaddingAttributes(this)
text = setData(data)
applySelector(this)
extracted()
}

}
private fun CheckedTextView.extracted() {
checkMarkDrawable?.setBounds(
0,
0,
checkMarkDrawable.intrinsicWidth,
checkMarkDrawable.intrinsicHeight
)
gravity = Gravity.CENTER_VERTICAL
setPadding(10, 10, 10, 10)
}
}

private fun toggleItemSelection(position: Int) {
if (selectedItemsPositions.contains(position)) {
selectedItemsPositions.remove(position)
} else {
selectedItemsPositions.add(position)
}
notifyItemChanged(position)
onMultiSelectionClicked?.invoke(data.filterIndexed { index, _ -> selectedItemsPositions.contains(index) })

}
}
private fun applySelector(textView: CheckedTextView) {

textView.setTextColor(primaryTextColor)
textView.setCheckMarkDrawable(checkSelector)

}

fun setOnMultiSelection(callback: (List<RadioGroupData>) -> Unit) {
onMultiSelectionClicked = callback
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ss.smartfilterlib.multiselection.chipgroup
package com.ss.smartfilterlib.multiselection

import android.content.Context
import android.content.res.ColorStateList
Expand Down Expand Up @@ -57,7 +57,7 @@ class MultiselectionChipGroup @JvmOverloads constructor(context: Context, attrs:
}

fun setData(chipData: List<RadioGroupData>,chipType: MultiChipType,orientation: Int,bgSelector: Int,textSelector: Int, checkedChangedListener: ( List<Int>) -> Unit ) {
var chipIds = chipData.map { it.id } // Update chipIds property
var chipIds = chipData.map { it.id }

chipGroup.removeAllViews()
this.checkedChangedListener = checkedChangedListener
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading