在Recycler视图

时间:2017-11-07 20:56:06

标签: java android xml checkbox

我有多个投票选项,用户只能选择一个选项。我正在使用复选框和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;

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"/>

1 个答案:

答案 0 :(得分:0)

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


}