2019-10-09
How to hide recyclerview's all item in activity
stackoverflow
Question

I want to hide checkbox of all lists if I clicked one list within activity using recyclerview. But when I implement it, only the check box in the recyclerview list of the clicked position disappears. How can I hide the checkbox of the entire list? Of course it is possible on the reyclerview adapter, but I want this function to be implemented in activity.

In activity, the adapter was declared:

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerViewAdapter = new RecyclerViewAdapter(this, patientList, selected_patientList);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(recyclerViewAdapter);

And I wrote the code that hides the check box when click the recyclerview list in activity :

    recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox) ;
            checkBox.setVisibility(View.GONE);
        }

And my recyclerview adapter code is

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private Activity activity;
public ArrayList<PatientItem> patientList = new ArrayList<>();
public ArrayList<PatientItem> selected_patientList = new ArrayList<>();
Context mContext;
boolean checkboxIsVisible = true ;

public RecyclerViewAdapter(Context context, ArrayList<PatientItem> patientList, ArrayList<PatientItem> selected_patientList) {
    this.mContext = context;
    this.patientList = patientList;
    this.selected_patientList = selected_patientList;
}

public class MyViewHolder extends RecyclerView.ViewHolder {
    TextView patientType;
    TextView clinicID;
    TextView patientName;
    TextView dateFirst;
    TextView dateFinal;
    CheckBox checkBox ;
    ConstraintLayout cl_listitem;

    public MyViewHolder(View itemView) {
        super(itemView);
        patientType = (TextView) itemView.findViewById(R.id.typeViewItem);
        clinicID = (TextView) itemView.findViewById(R.id.clinicIDItem);
        patientName = (TextView) itemView.findViewById(R.id.patientNameItem);
        dateFirst = (TextView) itemView.findViewById(R.id.dateFirstItem);
        dateFinal = (TextView) itemView.findViewById(R.id.dateFinalItem);
        checkBox = (CheckBox) itemView.findViewById(R.id.checkBox) ;
        cl_listitem = (ConstraintLayout) itemView.findViewById(R.id.cl_listitem);

    }
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.patient_list_item, parent, false);

    final RecyclerViewAdapter.MyViewHolder vHolder = new RecyclerViewAdapter.MyViewHolder(itemView) ;
    return vHolder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    PatientItem data = patientList.get(position);
    holder.patientType.setText(data.getPatientType());
    holder.clinicID.setText(data.getClinicID());
    holder.patientName.setText(data.getPatientName());
    holder.dateFirst.setText(data.getDateFirst());
    holder.dateFinal.setText(data.getDateFinal());
    holder.checkBox.setChecked(data.isDeleteBox());
    holder.checkBox.setVisibility(checkboxIsVisible?View.VISIBLE:View.GONE);

    if(data.getPatientType() == "P"){
        holder.patientType.setText("");
        holder.patientType.setBackground(ContextCompat.getDrawable(mContext, R.drawable.parkinson));

    }
    else if(data.getPatientType() == "ET"){
        holder.patientType.setText("");
        holder.patientType.setBackground(ContextCompat.getDrawable(mContext, R.drawable.essential_tremor));
    }
    else{
        holder.patientType.setText("ㅡ");
    }
}


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

public void clear() {
    int size = patientList.size() ;
    patientList.clear() ;
    notifyItemRangeRemoved(0, size);
}


//어댑터 정비
public void refreshAdapter() {
    this.selected_patientList = selected_patientList;
    this.patientList = patientList;
    this.notifyDataSetChanged();
}

}

Answer
1

You can use a communicator (interface) to do this simply.

  • Create an interface and communicate with the activity with necessary arguments passing from the adapter.
  • Next you have to map your selected/checked item position from the list into corresponding model class. (i.e: isChecked = false)
  • OnItemClick from the list populate the corresponding item's isChecked value to true and lastly call adapter's notifyDataSetChanged()
  • After that in onBindViewHolder() you will check if the item's isChecked value and then show/hide the checkBox of the corresponding item behavior.

**In your PatientItemmodel class: **
Add this:

private boolean isChecked;

public boolean getChecked() {
    return isChecked;
}

public void setChecked(boolean checked) {
    isChecked = checked;
}

Create an interface inside/outside your Adapter class:

interface ItemClickCallBack{
    void onItemClicked(PatientItem item, int position);
}

Initialize it:

private ItemClickCallBack itemClickCallBack;

    Adapter(ItemClickCallBack callback) {
        this.itemClickCallBack = callback;
    }

Now in your onBindViewHolder() method,

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.bind(position);
}

Your Probable ViewHolder Class:

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

CheckedTextView mCheckedTextView;

ViewHolder(View itemView) {
    super(itemView);
    mCheckedTextView = itemView.findViewById(R.id.checked_text_view);
    itemView.setOnClickListener(this);
}

void bind(int position) {
    // check the state of the model
    if (!items.get(position).getChecked()) {
        mCheckedTextView.setChecked(false);}
    else {
        mCheckedTextView.setChecked(true);
    }
    mCheckedTextView.setText(String.valueOf(items.get(position).getPosition()));
}

@Override
public void onClick(View v) {
    int adapterPosition = getAdapterPosition();
    itemClickCallBack.onItemClicked(items.get(adapterPosition), adapterPosition);
}

}

** In your MainActivity.java:**

public class MainActivity extends AppCompatActivity implements ItemClickCallBack {

    public RecyclerView recyclerView;
    private Adapter adapter;
    private LinearLayoutManager layoutManager;

    List<PatientItem> items = new ArrayList<>();

/**TODO:
 * You can pass a view instance to this callback method to 
 * do operation other wise you can do it into your adapter 
 * setting some flags (i.e: Boolean) in here and get it from Adapter with another callback. 
 * */

    @Override
    public void onItemClicked(PatientItem item, int position) {
        if (!item.getChecked()) {
            //Do your actions
            item.setChecked(true);
        }
        else  {
            // Do hide/show your view
            //mCheckedTextView.setChecked(false);
            item.setChecked(false);
        }
    }
  // rest of your codes
}

Hope it helps
Feel free to ask any question. Happy coding

How to hide recyclerview's all item in activity
See more ...