Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over the view items inside a recyclerview?

Tags:

android

I have a RecyclerView with the following adapter:

public class DataTransferMeasureListAdapter extends DataTransferBaseListAdapter<DataTransferMeasureListAdapter.ViewHolder> {
    private Context context = null;
    private List<DataTransferListEntry> entries = null;

    public DataTransferMeasureListAdapter(Context context, List<DataTransferListEntry> entries) {
        super(context);

        this.context = context;
        this.entries = entries;
    }

    @Override
    public DataTransferMeasureListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.fragment_datatransfer_measure_list_row, parent, false);

        return new DataTransferMeasureListAdapter.ViewHolder(this.context, itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final DataTransferMeasureListEntry entry = (DataTransferMeasureListEntry) entries.get(position);
        final ViewHolder viewHolder = (ViewHolder) holder;

        String time = Helper.getDateTimePattern(this.context, entry.getDate());
        String value = Helper.getMeasureValue(unit, entry.getValue());

        viewHolder.textViewTime.setText(time);
        viewHolder.textViewValue.setText(value);
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private Context context = null;
        private TextView textViewValue = null;
        private TextView textViewTime = null;

        public ViewHolder(Context context, View itemView) {
            super(itemView);

            this.context = context;

            this.textViewValue = (TextView) itemView.findViewById(R.id.datatransfer_measure_list_row_textview_value);
            this.textViewTime = (TextView) itemView.findViewById(R.id.datatransfer_measure_list_row_textview_time);
        }
    }
}

In my ListView:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    this.adapter = new DataTransferMeasureListAdapter(this.getActivity(), this.dataTransferMeasureListEntries);
    this.listRowParent = (RecyclerView) view.findViewById(R.id.datatransfer_measure_list_row_parent);
    this.listRowParent.setAdapter(this.adapter);
    this.listRowParent.setLayoutManager(new LinearLayoutManager(this.getActivity()));
}

I now need a possibility to iterate over each Viewholder inside my adapter so that i can change the visibility of some view elements. I need to set it from outside of the adapter.

How can i do that?

like image 866
Mulgard Avatar asked Aug 01 '15 09:08

Mulgard


1 Answers

As @pskink wrote in the comment. The fastest and correct way to achieve this is by changing the entiries data so that it will contain the values that you will set the visibilty by and then change those from your fragment and call notifyItemChanged in case one data entry ahs changed or notifyItemRangeChanged if a few have changed or notifyDataSetChanged if everything changed or a sporadic change has occured.

You dont want the fragment to call methods of the adapter, you want it only to "know" and manipulate the data.

like image 174
EE66 Avatar answered Oct 31 '22 22:10

EE66