在选择一个项目时,为 Groupie RecyclerView 取消选择所有其他项目

On selecting an item deselect all others for Groupie RecyclerView

我看到这个问题被问过几次,但可能是因为我正在使用 Groupie 库将项目添加到我的适配器,当其中一个项目被选中时我无法取消选择其他项目(类似单选按钮功能)。我正在尝试将更改通知适配器并在某种程度上获得所选项目的位置,但我认为如果有人可以为我指出这一点,我可能仍然遗漏了一些东西。

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)


    rvOptions.layoutManager = LinearLayoutManager(activity)
    adapter = GroupAdapter()
    rvOptions.adapter = adapter


    viewModel = ViewModelProviders.of(activity as MainActivity).get(TestViewModel::class.java)
    val myViewModel = ViewModelProvider(
        this, MyViewModelFactoryForHashMap(arguments?.getString("headerText"))
    ).get(TestViewModel::class.java)

    myViewModel.userMutableLiveData.observe(this, userListUpdateObserver)

}


private val userListUpdateObserver: Observer<Array<ModelTest>?> =
    Observer { userArrayList ->
        for (s in userArrayList!!) {
            adapter.add(
                RecyclerDialogOptionsItem(
                    activity as MainActivity,
                    s.title,
                    s.selected!!,
                    this@MyDialogFragment
                )
            )
        }
    }

override fun onClickItem( position: Int) {

    adapter.notifyDataSetChanged()
}

}

这里是我的适配器,这样的想法是,当一个项目处于选中状态时,与其关联的 imageView 将可见,而所有其他项目将变得不可见。

class RecyclerDialogOptionsItem(
private val activity: MainActivity,
private val title: String?,
private var selected: Boolean,
private val adapterListener: AdapterListener
) : Item<GroupieViewHolder>() {

companion object {
    var clickListener: AdapterListener? = null
}


override fun bind(viewHolder: GroupieViewHolder, position: Int) {

    var selected_position = -1;

    viewHolder.apply {

        with(viewHolder.itemView) {

            tvTitle.text = title

            clickListener = adapterListener

            itemView.setOnClickListener {

                clickListener?.onClickItem(adapterPosition)


                selected = true

                selected_position = position
                if (selected) {

                    ivChecked.visible()

                } else {
                    ivChecked.invisible()
                }

            }


        }

        selected_position = adapterPosition
        
    }

}


override fun getLayout() = R.layout.rv_options_item_row


interface AdapterListener {
    fun onClickItem(position: Int)
}

}

非常感谢。

第一个选项:创建适配器并在适配器中创建一个局部变量并检查选择的位置它是变量。

第二个选项: 在名为 selected_position 的 Activity 中创建变量。 当调用 onClickItem 函数时更新此变量并通知适配器更改。 在您的项目中检查 activity 中的变量,例如:

// Activity
selected_position = -1

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)


    rvOptions.layoutManager = LinearLayoutManager(activity)
    adapter = GroupAdapter()
    rvOptions.adapter = adapter


    viewModel = ViewModelProviders.of(activity as MainActivity).get(TestViewModel::class.java)
    val myViewModel = ViewModelProvider(
        this, MyViewModelFactoryForHashMap(arguments?.getString("headerText"))
    ).get(TestViewModel::class.java)

    myViewModel.userMutableLiveData.observe(this, userListUpdateObserver)

}


private val userListUpdateObserver: Observer<Array<ModelTest>?> =
    Observer { userArrayList ->
        for (s in userArrayList!!) {
            adapter.add(
                RecyclerDialogOptionsItem(
                    activity as MainActivity,
                    s.title,
                    s.selected!!,
                    this@MyDialogFragment
                )
            )
        }
    }

override fun onClickItem( position: Int) {
    selected_position = position
    adapter.notifyDataSetChanged()
}

}
// Item

class RecyclerDialogOptionsItem(
private val activity: MainActivity,
private val title: String?,
private var selected: Boolean,
private val adapterListener: AdapterListener
) : Item<GroupieViewHolder>() {

companion object {
    var clickListener: AdapterListener? = null
}


override fun bind(viewHolder: GroupieViewHolder, position: Int) {


    viewHolder.apply {

        with(viewHolder.itemView) {

            tvTitle.text = title

            clickListener = adapterListener
            if (activity.selected_position == position) {
                    ivChecked.visible()

                } else {
                    ivChecked.invisible()
                }

            itemView.setOnClickListener {
                clickListener?.onClickItem(adapterPosition)

            }


        }

        
    }

}


override fun getLayout() = R.layout.rv_options_item_row


interface AdapterListener {
    fun onClickItem(position: Int)
}

}