简体   繁体   中英

How to create an algorithm to set a multi checked items in a RecyclerView

I need to create an algorithm that set a checked item in a RecyclerView. I have a RecyclerView made with boxes with a TextView and an ImageView, I use the TextView to show nameItems from a List and the ImageView to show a check once the user clicks on it. What I want to do is that every time the user clicks on an item the check appears and every time he clicks on a checked Item the check disappears.

I create an algorithm which uses a boolean variable (isChecked) set to false, every time user clicks on an item the variable is set to true and vice versa. In this case, a user has to click on the next item of the list two times to let show the check. How can I do it? Thank you so much in advance

Here is my Adapter's class:

public class RecyclerTypeListViewAdapter extends RecyclerView.Adapter<RecyclerTypeListViewAdapter.TypeViewHolder> {

    List<TipologiaEvento> eventType;
    private boolean isChecked = false;

    public RecyclerTypeListViewAdapter (List<TipologiaEvento> typeList){
        this.eventType = typeList;
    }

    public static class TypeViewHolder extends RecyclerView.ViewHolder {
        LinearLayout linearLayout;
        TextView typeName;
        ImageView check_icon;

        TypeViewHolder (View view){
            super(view);
            linearLayout = view.findViewById(R.id.type_listed_linear_layout);
            typeName = view.findViewById(R.id.event_type_text_view);
            check_icon = view.findViewById(R.id.event_type_checked_icon);


        }
    }

    @Override
    public TypeViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cell_type_listed, viewGroup, false);
        TypeViewHolder tvh = new TypeViewHolder(view);
        return tvh;
    }

    @Override
    public void onBindViewHolder(final TypeViewHolder typeViewHolder, int position) {
        typeViewHolder.typeName.setText(eventType.get(position).getDescrizione());

        typeViewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isChecked == false) {
                    typeViewHolder.check_icon.setVisibility(View.VISIBLE);
                    Toast.makeText(v.getContext(), "Aggiunto: " + typeViewHolder.typeName.getText().toString(), Toast.LENGTH_SHORT).show();
                    isChecked = true;
                } else {
                    typeViewHolder.check_icon.setVisibility(View.INVISIBLE);
                    Toast.makeText(v.getContext(), "Rimosso: " + typeViewHolder.typeName.getText().toString(), Toast.LENGTH_SHORT).show();
                    isChecked = false;
                }
            }
        });
    }

}

Just take one boolean value in the model class of your list. and in bindviewholder. just put the condition like below

if(list.get(position).ischecked()) {
    action of checked
} else {
    action of unchecked
}

and on the click of the item you just have to change the flag of object and notify the adapter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM