Skip to content

单选 多选 滑动选择

angcyo edited this page Nov 10, 2019 · 3 revisions

单选多选默认已经提供了支持.

滑动选择需要额外的组件支持.

效果图如下:

单选 多选 滑动选择

单选 多选

默认情况下, 并没有开启选择模式.

开启如下:

dslAdapter.itemSelectorHelper.selectorModel = MODEL_NORMAL
dslAdapter.itemSelectorHelper.selectorModel = MODEL_SINGLE
dslAdapter.itemSelectorHelper.selectorModel = MODEL_MULTI
/** 正常 状态 */
const val MODEL_NORMAL = 0

/** 单选 状态 */
const val MODEL_SINGLE = 1

/** 多选 状态 */
const val MODEL_MULTI = 2

选择回调监听

实现回调接口:

interface OnItemSelectorListener {

    /**选择模式改变*/
    fun onSelectorModelChange(@MODEL from: Int, @MODEL to: Int) {
        L.i("选择模式改变:[${from._modelToString()}]->[${to._modelToString()}]")
    }

    /**
     * @param selectorItems 选中的项
     * @param selectorIndexList 选中项在数据源中的索引
     * @param isSelectorAll 是否全部选中
     * */
    fun onSelectorItemChange(
        selectorItems: MutableList<DslAdapterItem>,
        selectorIndexList: MutableList<Int>,
        isSelectorAll: Boolean,
        selectorParams: SelectorParams
    ) {
        L.i("选择改变->${selectorIndexList}")
    }
}

设置回调接口:

dslAdapter.itemSelectorHelper.onItemSelectorListener = OnItemSelectorListener()

常用操作api

固定选择项:

itemSelectorHelper.fixedSelectorItemList = fixedItemList

选中某项:

itemSelectorHelper.selector(SelectorParams(item, true.toSelectOption()))

选中多项:

itemSelectorHelper.selector(itemList, SelectorParams(selector = true.toSelectOption()))

选中范围:

itemSelectorHelper.selector(0..10, SelectorParams(selector = true.toSelectOption()))

选中所有:

itemSelectorHelper.selectorAll(SelectorParams(selector = true.toSelectOption()))

所有可选配置参数SelectorParams:

data class SelectorParams(

    /**操作的 目标*/
    var item: DslAdapterItem? = null,

    /**操作*/
    @SELECTOR
    var selector: Int = OPTION_SELECT,

    /**
     * 是否要通知事件
     * [com.angcyo.dsladapter.ItemSelectorHelper._notifySelectorChange]
     * */
    var notify: Boolean = true,

    /**
     * 是否需要回调[_itemSelectorChange]
     * [com.angcyo.dsladapter.ItemSelectorHelper._selectorInner]
     * */
    var notifyItemChange: Boolean = true,

    /**
     * 传递给
     * [com.angcyo.dsladapter.DslAdapterItem._itemSelectorChange]
     * */
    var updateItemDepend: Boolean = false,

    //额外自定义的扩展数据
    var extend: Any? = null,

    //使用过滤后的数据源
    var _useFilterList: Boolean = true,

    /**
     * 当调用
     * [selector(kotlin.ranges.IntRange, com.angcyo.dsladapter.SelectorParams)]
     * or
     * [selector(java.util.List<? extends com.angcyo.dsladapter.DslAdapterItem>, com.angcyo.dsladapter.SelectorParams)]
     * 操作列表为空时, 是否继续通知事件.
     *
     * 需要优先开启[notify]
     * */
    var notifyWithListEmpty: Boolean = false
)

滑动选择

滑动选择需要组件SlidingSelectorHelper的支持, 继承自RecyclerView.OnItemTouchListener, 非常轻量强大.

开启功能:

recyclerView.addOnItemTouchListener(SlidingSelectorHelper(applicationContext, dslAdapter))

在界面上, 长按Item, 即可进入滑动选择模式, 手指在顶部or底部会触发滑动.

顶部or底部距离越近, 会智能滑动提速.