如何从另一个项目更新recyclerview项目中的数据? [英] how to update data in recyclerview item from another item?
问题描述
我在RecyclerView
上遇到问题,我在RecyclerView
中有一个radio button
,并且每个行项目中都有其他几个视图,
I have a problem with RecyclerView
, I have RecyclerView
which has a radio button
and few other views in each row item,
我真正想要的是,当用户选中了RadioButton
时,我想取消选中其他RadioButton
(如果之前进行了检查).由于它是Recyclerview
,因此我无法使用广播组.
What I wanted exactly is, when a RadioButton
is checked by user I want to uncheck other RadioButton
(if anything is checked earlier). Since it is a Recyclerview
I cannot use radiogroup.
在适配器onBindViewHolder
中,我为每个单选按钮编写此侦听器
In the adapter onBindViewHolder
I write this listener for each radio button
holder.radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedPosition = holder.getAdapterPosition();
Toast.makeText(activity, "section: " + selectedPosition, Toast.LENGTH_SHORT).show();
}
});
如何取消选中以前的单选按钮?
换句话说,如何从另一个项目视图侦听器更新recyclerview特定项目中的视图属性?
how can i make previous radio button selected uncheck?
in other words, how can i update view property in specific item of recyclerview from another item view listener?
推荐答案
您需要全局引用适配器中的selectedItem,然后在用户检查新的单选按钮时更新所有项目.
You need to have a reference to the selectedItem globally in your adapter and then update all the items when the user checks a new radio button.
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private int selectedItemPosition = -1;
}
然后在适配器的onBindViewHolder()
方法中进行操作
And do things in your onBindViewHolder()
method of the adapter,
@Override
public void onBindViewHolder(final Adapter.ViewHolder holder, int position) {
holder.radioButton.setChecked(holder.getAdapterPosition()==selectedItemPosition);
holder.radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedItemPosition = holder.getAdapterPosition();
notifyDataSetChanged();
}
});
}
这篇关于如何从另一个项目更新recyclerview项目中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!