3 回答
TA贡献1816条经验 获得超4个赞
尝试在您的适配器中为每个单选按钮使用 if else 如下所示
if(questionArrayList.get(position).getChoice1Selected())
{
viewHolder.choice1.setSelected(true);
}else{
viewHolder.choice1.setSelected(false);
}
TA贡献1811条经验 获得超4个赞
试试这个代码并根据您的代码进行更改,并将一个布尔字段带入问题 pojo 类..
public class RecyclerViewAdpater extends RecyclerView.Adapter<RecyclerViewAdpater.ItemViewHolder> {
List<Question> mQuestionList = new ArrayList<>();// hear you can pass any pojo class object.
Context mContext;
OnItemClick onItemClick;
public void setOnItemClick(OnItemClick onItemClick) {
this.onItemClick = onItemClick;
}
public interface OnItemClick {
void getPosition(Question data); //pass any data to shared it.
}
public RecyclerViewAdpater(List<Question> mQuestionList, Context mContext) {
this.mQuestionList = mQuestionList;
this.mContext = mContext;
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main, parent, false);
return new ItemViewHolder(view);
}
@Override
public void onBindViewHolder(final ItemViewHolder holder, final int position) {
// below code handle click event on recycler view item.
final Question str=mQuestionList.get(position); // here your boject
holder.textView.setText(str.getName());
if (str.isSelected()){
// hear set your radio button select or unselect.
holder.textView.setVisibility(View.GONE);
}
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClick.getPosition(str);
}
});
}
@Override
public int getItemCount() {
return mQuestionList.size();
}
public class ItemViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public ItemViewHolder(View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.textView);
}
}
}
在适配器定义之后,它意味着适配器绑定到回收器视图并且适配器不为空然后调用下面的代码..
adpater.setOnItemClick(new RecyclerViewAdpater.OnItemClick() {
@Override
public void getPosition(Question data) {
// perform any operation here.
data.setSelected(true);
adpater.notifyDataSetChanged();
}
});
如果您为您引用相同的鳕鱼,则只为 mange select 或 un select 单选按钮取一个布尔值。
添加回答
举报