Reputation: 4164
I see this question asked a few times but maybe because I'm using the Groupie library for adding items to my adapter, I'm not being able to unselect the other items when one of the them is selected (similar to radio button functionality). I'm trying to notify the adapter of changes and somewhat get the position of the item selected but I think I may still be missing something if someone could point this out for me please.
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()
}
}
And here my adapter so that the idea is when one of an item is under the selected state the imageView associated to it would be visible and for all the others would become invisible.
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)
}
}
Many thanks.
Upvotes: 0
Views: 1189
Reputation: 141
First option: Create your adapter and create a local variable in the adapter and check the position selected it is the variable.
Second option: Create the variable in the Activity called selected_position. When the function onClickItem called update this variable and notify adapter change. in your item check the variable in the activity, example:
// 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)
}
}
Upvotes: 2