解决listview setSelection 不生效

转自:https://blog.csdn.net/qq_16774265/article/details/51957083

class SelectPosListView : ListView {

    lateinit var dataChangedListener: DataChangedListener

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(context, attrs, 0, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)

    override fun handleDataChanged() {
        super.handleDataChanged()
        dataChangedListener.onSuccess()
    }

    fun setDataChangeDListener(dataChangedListener: DataChangedListener) {
        this.dataChangedListener = dataChangedListener
    }

    interface DataChangedListener {
        fun onSuccess()
    }
}

调用 这是在adapter内调用的

fun setPositionToSelect(selectedItem: Any){
    var index = itemList.indexOf(selectedItem)
    listCtrl.setDataChangeDListener(object :SelectPosListView.DataChangedListener{
        override fun onSuccess() {
            listCtrl.setSelection(index - emptyItemNum)
        }
    })
}

如果在activity 内调用直接  listview.setxxxxx.....就okk!

你可能感兴趣的:(坑)