【问题标题】:get selected single checkbox in recycler view在回收站视图中选中单个复选框
【发布时间】:2018-04-20 09:46:06
【问题描述】:

我有多个投票选项,用户只能选择一个选项。我在带有复选框和文本视图的回收站视图中获得选项。 如果用户选择另一个复选框并通过按提交按钮获取选定的文本,我想取消选中之前选中的复选框。

这是我的回收站视图代码。

public class Poll_Options_recyclerView_Adapter extends RecyclerView.Adapter<Poll_Options_recyclerView_Adapter.MyViewHolder> {
public List<Poll_Option_Result> result = new ArrayList<>();
Context mycontext;

public Poll_Options_recyclerView_Adapter(List<Poll_Option_Result> result, Context context) {
    this.result = 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();
}
}

这是我的回收站视图自定义 xml 代码。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="5"
    >
    <CheckBox
        android:id="@+id/check_box"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"/>
<TextView
    android:layout_width="0dp"
    android:layout_weight="4.5"
    android:layout_height="wrap_content"
    android:text="xyz"
    android:layout_marginLeft="20dp"
    android:textColor="@color/colortoolbartitle"
    android:padding="5dp"
    android:id="@+id/optionA"/>

【问题讨论】:

    标签: java android xml checkbox


    【解决方案1】:
    private int mCheckedPostion = -1;// no selection by default
    

    通过在 onBindViewHolder 中添加此代码来完成

    @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();
                }
            }
        });
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-30
      • 2016-05-15
      • 1970-01-01
      • 2020-08-02
      • 2017-02-11
      • 1970-01-01
      • 2020-08-06
      相关资源
      最近更新 更多