我正在开发一款具有投票功能的应用。 在民意调查中,有多种选择。选项可以是3,4,5等,用户只能选择一个选项。
我有自定义类的回收站视图,它有一个复选框和textview。问题是:
用户现在可以选择多个选项。
我想通过选择其他单选按钮来更改选择。
这是我的回收者视图代码
public class Poll_Options_recyclerView_Adapter extends RecyclerView.Adapter<Poll_Options_recyclerView_Adapter.MyViewHolder> {
public List<Poll_Option_Result> result = new ArrayList<>();
Context mycontext;
private int mCheckedPostion = -1; public Poll_Options_recyclerView_Adapter(列表结果,上下文上下文){ this.result =结果; this.mycontext = context; }
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_option_rec_list, parent, false);
return new MyViewHolder(itemView, mycontext, result);
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView poll_option;
CheckBox checkBox;
List<Poll_Option_Result> result_List = new ArrayList<>();
Context mycontext;
public MyViewHolder(View itemView, Context mycontext, List<Poll_Option_Result> result_List) {
super(itemView);
this.result_List = result_List;
this.mycontext = mycontext;
poll_option = (TextView) itemView.findViewById(R.id.optionA);
checkBox=(CheckBox)itemView.findViewById(R.id.check_box);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
}
}
@Override
public void onBindViewHolder(Poll_Options_recyclerView_Adapter.MyViewHolder holder, int position) {
final Poll_Option_Result ResultList = result.get(position);
holder.poll_option.setText(ResultList.getOption());
}
@Override
public int getItemCount() {
return result.size();
}
}
编辑:通过将此代码添加到onBindView来解决问题
@Override
public void onBindViewHolder(final Poll_Options_recyclerView_Adapter.MyViewHolder holder, final int position) {
final Poll_Option_Result ResultList = result.get(position);
holder.poll_option.setText(ResultList.getOption());
//check checkbox and uncheck previous selected button
holder.checkBox.setChecked(position == mCheckedPostion);
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (position == mCheckedPostion) {
holder.checkBox.setChecked(false);
StringGen.username = "";
mCheckedPostion = -1;
} else {
mCheckedPostion = position;
StringGen.username = holder.poll_option.getText().toString();
Toast.makeText(mycontext, "select : "+position+holder.poll_option.getText(), Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
}
}
});
}
答案 0 :(得分:1)
如果您希望将单选按钮作为回收站视图项的一部分,则可以在回收器视图适配器中记住上次选择的项目的位置,并在选择下一项时取消设置。
这样的东西,最简单的方法:
class MyAdapter : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
var items: MutableList<Item> = mutableListOf()
private var lastPos: Int? = null
private var checkBox: CheckBox? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder =
MyViewHolder(createHolderView(parent, R.layout.item))
override fun getItemCount(): Int = items.size
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val item = items[position]
holder.bind(item)
holder.itemView.checkbox.setOnCheckedChangeListener { view, isChecked ->
if (isChecked) {
if (lastPos != null) {
checkBox?.isChecked = false
}
lastPos = holder.adapterPosition
checkBox = view as CheckBox
}
}
}
private fun createHolderView(parent: ViewGroup, @LayoutRes itemLayoutId: Int): View =
LayoutInflater.from(parent.context).inflate(itemLayoutId, parent, false)
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(item: Item) {
itemView.txtTitle.text = item.name
}
}
我使用复选框而不是radiobutton,我认为它运作良好。和项目布局:
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/txtTitle"
tools:text="Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
希望它有所帮助。