从recyclerView中选择一个复选框,然后取消选择先前选择的复选框

时间:2018-11-22 09:54:49

标签: java android android-recyclerview android-checkbox

我在回收者视图中有部分内容。该项目包括行和标题。我想选择一个带有复选框的项目,如果用户选择了下一个,我想取消选择旧的(复选框)。我找不到任何地方可以实现此功能。

这是我的代码

recyclerView = (RecyclerView)findViewById(R.id.recyclerViewActivityExample);
    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

    list = new ArrayList<>();
    list.add(new AnimalObject("Cat","Mammals", false));
    list.add(new AnimalObject("Lion","Mammals", false));
    list.add(new AnimalObject("Dog","Mammals", false));
    list.add(new AnimalObject("Monkey","Mammals", false));
    list.add(new AnimalObject("Puma","Mammals", false));

    list.add(new AnimalObject("Albatross","Birds", false));
    list.add(new AnimalObject("Pigeon","Birds", false));

    list.add(new AnimalObject("Crabs","Aquatic Animals", false));
    list.add(new AnimalObject("Sharks","Aquatic Animals", false));

    MyAdapter myAdapter = new MyAdapter();


    sectionedRecyclerViewAdapter = new SectionedRecyclerViewAdapter(getApplicationContext(),
            R.layout.layout_list_section, R.id.textViewItemSection, myAdapter, this);
    sectionedRecyclerViewAdapter.setSections(list);

    recyclerView.setAdapter(sectionedRecyclerViewAdapter);

}





 //-------------------Adapter----------------------------
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> implements MyViewHolder.ViewHolderClickListener {

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.
                from(parent.getContext()).
                inflate(R.layout.layout_list_item, parent, false);

        return new MyViewHolder(itemView,this);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        AnimalObject animalObject = list.get(position);

        holder.title.setText(animalObject.name);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // checkbox.setChecked(true);
                row_index = position;
                notifyDataSetChanged();
            }
        });

        if (row_index == position) {
            holder.checkBox.setChecked(true);
        } else {
            holder.checkBox.setChecked(false);
        }
    }

    @Override
    public void onClick(View v) {

        Toast.makeText(getApplicationContext(), list.get(sectionedRecyclerViewAdapter.getIndexForPosition(recyclerView.getChildPosition(v))).name,Toast.LENGTH_SHORT).show();

    }
}

//适配器

 public String name;
public String type;
public boolean ischecked ;

public AnimalObject(final String name, final String type, boolean ischecked){

    this.name = name ;
    this.type = type ;
    this.ischecked = ischecked;
}
public boolean ischecked() {
    return ischecked;
}

public void setIschecked(boolean ischecked) {
    this.ischecked = ischecked;
}

3 个答案:

答案 0 :(得分:2)

在您的Adapter

使用此:
  private int row_index;

   @Override
    public void onBindViewHolder(final Main_Page_Payment_Cash_Adapter.ViewHolder holder, final int position) {

             holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // checkbox.setChecked(true);
                    row_index = position;
                    notifyDataSetChanged();
                }
            });

            if (row_index == position) {
              // checkbox.setChecked(true);
            } else {
               // checkbox.setChecked(false);
            }
    }

我希望这是您想要的。

答案 1 :(得分:1)

 @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        AnimalObject animalObject = list.get(position);

        holder.title.setText(animalObject.name);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AnimalObject animalObject = list.get(position);
                int currentCheckedStatus = animalObject.ischecked();
                for (int i =0;i<list.size();i++)
                {

                    if (i==position)
                    {
                        animalObject.setIschecked(!currentCheckedStatus);
                    }
                    else
                    {
                        animalObject.setIschecked(false);
                    }
                }
                notifyDataSetChanged();
            }
        });

            holder.checkBox.setChecked(animalObject.ischecked());
    }

答案 2 :(得分:0)

完整示例
公共类ChildAddressAdapter扩展了RecyclerView.Adapter {

private Activity context;
private List<AddressDetail> addressDetailList;
private int selectedPosition = -1;

public ChildAddressAdapter(Activity context, List<AddressDetail> addressDetailList) {
    this.context = context;
    this.addressDetailList = addressDetailList;
}

@NonNull
@Override
public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    LayoutInflater inflater = LayoutInflater.from(context);
    View myView = inflater.inflate(R.layout.address_layout, parent, false);
    return new CartViewHolder(myView);
}

@Override
public void onBindViewHolder(@NonNull CartViewHolder holder, int position) {

    holder.adress_checkbox.setOnClickListener(view -> {
        selectedPosition = holder.getAdapterPosition();
        notifyDataSetChanged();
    });

    if (selectedPosition==position){
        holder.adress_checkbox.setChecked(true);
    }
    else {
        holder.adress_checkbox.setChecked(false);
    }


}

@Override
public int getItemCount() {
    return  addressDetailList.size();
}

class CartViewHolder extends RecyclerView.ViewHolder
{
    TextView address_text,address_tag;
    CheckBox adress_checkbox;

    CartViewHolder(View itemView) {
        super(itemView);
        address_text = itemView.findViewById(R.id.address_text);
        address_tag = itemView.findViewById(R.id.address_tag);
        adress_checkbox = itemView.findViewById(R.id.adress_checkbox);
    }
}}