在RecyclerView中隐藏/显示项目

时间:2019-12-28 12:12:10

标签: android firebase android-recyclerview

在MainActivity中,我有RadioButtons和RecyclerView,它们通过适配器填充了两种数据类型。告诉我如何通过RadioButton隐藏/显示特定的数据类型。谢谢。

适配器

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    switch (viewType) {
        case Person.PersonType.TYPE_1:
            View userType1 = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.driver_row, parent, false);
            return new PersonType1ViewHolder(userType1);
        case Person.PersonType.TYPE_2:
            View userType2 = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.fare_row, parent, false);
            return new PersonType2ViewHolder(userType2);
    }
    return super.onCreateViewHolder(parent, viewType);
}

@Override
protected void populateViewHolder(RecyclerView.ViewHolder viewHolder, Person model,
                                  int position) {
    if (viewHolder instanceof PersonType1ViewHolder) {
        ((PersonType1ViewHolder) viewHolder).time.setText("Full time: " + model.getTime());
        return;
    }
    if (viewHolder instanceof PersonType2ViewHolder) {
        ((PersonType2ViewHolder) viewHolder).time.setText("Full time2: " + model.getTime());
    }

}
private class PersonType1ViewHolder extends RecyclerView.ViewHolder {
    TextView time;
    PersonType1ViewHolder(View itemView) {
        super(itemView);
        time = itemView.findViewById(R.id.tv_time);
    }
}
private class PersonType2ViewHolder extends RecyclerView.ViewHolder {
    TextView time;
    PersonType2ViewHolder(View itemView) {
        super(itemView);
        time = itemView.findViewById(R.id.tv_time_two);
    }
}
@Override
public int getItemViewType(int position) {
    return getItem(position).getType();
}

0 个答案:

没有答案