diff --git a/base-filters/src/main/java/ru/touchin/roboswag/base_filters/select_list_item/ListSelectionView.kt b/base-filters/src/main/java/ru/touchin/roboswag/base_filters/select_list_item/ListSelectionView.kt index 6ed5c7d..c19db8e 100644 --- a/base-filters/src/main/java/ru/touchin/roboswag/base_filters/select_list_item/ListSelectionView.kt +++ b/base-filters/src/main/java/ru/touchin/roboswag/base_filters/select_list_item/ListSelectionView.kt @@ -2,11 +2,15 @@ package ru.touchin.roboswag.base_filters.select_list_item import android.content.Context import android.util.AttributeSet +import android.view.ContextThemeWrapper import android.view.LayoutInflater -import android.widget.FrameLayout +import android.view.ViewGroup +import android.view.ViewGroup.LayoutParams.MATCH_PARENT +import android.view.ViewGroup.LayoutParams.WRAP_CONTENT +import androidx.annotation.StyleRes +import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import ru.touchin.roboswag.base_filters.databinding.SelectionItemBinding -import ru.touchin.roboswag.base_filters.databinding.SingleSelectionLayoutBinding import ru.touchin.roboswag.base_filters.select_list_item.adapter.BaseSelectionViewHolder import ru.touchin.roboswag.base_filters.select_list_item.adapter.HolderFactoryType import ru.touchin.roboswag.base_filters.select_list_item.adapter.SelectionItemViewHolder @@ -30,12 +34,15 @@ private typealias OnSelectedItemsListener = (items: List) -> class ListSelectionView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, - defStyleAttr: Int = 0, - defStyleRes: Int = 0 -) : FrameLayout(context, attrs, defStyleAttr, defStyleRes) + defStyleAttr: Int = 0 +) : RecyclerView(context, attrs, defStyleAttr) where ItemType : BaseSelectionItem, HolderType : BaseSelectionViewHolder { + enum class SelectionType { SINGLE_SELECT, MULTI_SELECT } + + constructor(context: Context, @StyleRes themeResId: Int) : this(ContextThemeWrapper(context, themeResId)) + private var mutableItems: List = emptyList() private var selectionType = SelectionType.SINGLE_SELECT @@ -43,6 +50,11 @@ class ListSelectionView @JvmOverloads constructor( private var onSelectedItemsChanged: OnSelectedItemsListener? = null private var factory: HolderFactoryType = getDefaultFactory() + init { + layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, WRAP_CONTENT) + layoutManager = LinearLayoutManager(context) + } + private fun getDefaultFactory(): HolderFactoryType = { parent, clickListener, selectionType -> SelectionItemViewHolder( binding = SelectionItemBinding.inflate(LayoutInflater.from(parent.context), parent, false), @@ -51,9 +63,7 @@ class ListSelectionView @JvmOverloads constructor( ) } - private val binding = SingleSelectionLayoutBinding.inflate(LayoutInflater.from(context), this, true) - - private val adapter by lazy { + private val selectionAdapter by lazy { SheetSelectionAdapter( onItemSelectAction = onItemSelectedListener, selectionType = selectionType, @@ -67,8 +77,13 @@ class ListSelectionView @JvmOverloads constructor( onSelectedItemsChanged?.invoke(mutableItems) } + fun updateItems(items: List) { + mutableItems = items + updateList() + } + private fun updateList() { - adapter.submitList(mutableItems) + selectionAdapter.submitList(mutableItems) } private fun updateAfterSelection(selectedItem: ItemType) { @@ -98,7 +113,7 @@ class ListSelectionView @JvmOverloads constructor( } fun addItemDecoration(itemDecoration: RecyclerView.ItemDecoration) = apply { - binding.itemsRecycler.addItemDecoration(itemDecoration) + this@ListSelectionView.addItemDecoration(itemDecoration) } fun onSelectedItemListener(listener: OnSelectedItemListener) = apply { @@ -114,10 +129,8 @@ class ListSelectionView @JvmOverloads constructor( } fun build() = this@ListSelectionView.also { - binding.itemsRecycler.adapter = adapter + it.adapter = selectionAdapter updateList() } } - - enum class SelectionType { SINGLE_SELECT, MULTI_SELECT } } diff --git a/base-filters/src/main/res/layout/single_selection_layout.xml b/base-filters/src/main/res/layout/single_selection_layout.xml deleted file mode 100644 index 2db23f9..0000000 --- a/base-filters/src/main/res/layout/single_selection_layout.xml +++ /dev/null @@ -1,10 +0,0 @@ - -