如何在活动中隐藏recyclerview的所有项目

时间:2019-09-20 16:52:15

标签: android android-recyclerview recycler-adapter

如果我使用recyclerview在活动中单击一个列表,我想隐藏所有列表的复选框。 但是,当我实现它时,只有单击位置的recyclerview列表中的复选框消失了。如何隐藏整个列表的复选框? 当然可以在reyclerview适配器上使用,但我希望在活动中实现此功能。

在活动中,已声明适配器:

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

然后,我编写了在活动中单击recyclerview列表时隐藏复选框的代码:

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

我的recyclerview适配器代码是

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

}

1 个答案:

答案 0 :(得分:-1)

我这样解决了这个问题。

我在RecyclerViewAdapter上声明了onBindViewHolder:

holder.checkBox.setVisibility(checkboxIsVisible?View.VISIBLE:View.GONE);

而且,我在RecyclerViewAdapter中声明了visible()方法:

public void visible(){
    checkboxIsVisible = false ;
    this.notifyDataSetChanged();
}

然后,我在活动中使用可见方法:

recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            recyclerViewAdapter.visible();
        }

运行此命令时,当我单击一个列表时,列表中的所有复选框都会消失。