i'm new with Recyclerviews and i'm trying to modify the viewHolder from a function outside the OnBindViewHolder ( i'm going to change the background color of a viewHolder).
i tried : - to save the ViewHolders in a Arraylist of ViewHolders and change them. - to get the view from the recycler view directly. - get the view holder by id directly and modify it. And many more.. Everything i tried failed and i have found out for some reason some views work and i succeeded on changing their colors but then i get an error telling me that some views are null. i tried to add my view from onBindViewHolder but it seems that my views get called multiple times when i scroll. my lastest solution is to add the viewholder to a list from the onCreateViewHolder method. It seems for some reason the ViewHolders get saved when the recyclerview is fully scrolled and i have no idea what is going on.. please help. this is the code for the OncreateViewHolder method :
@Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view ;
switch (viewType) {
case item_nature.IF_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.if_layout, parent, false);
IFViewHolder v = new IFViewHolder(view);
Holders.add(v);
return v;
case item_nature.ELSE_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.else_layout, parent, false);
ELSEViewHolder v1 = new ELSEViewHolder(view);
Holders.add(v1);
return v1;
case item_nature.THEN_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.then_layout, parent, false);
THENViewHolder v2 = new THENViewHolder(view);
Holders.add(v2);
return v2;
case item_nature.END_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.end_layout, parent, false);
ENDIFViewHolder v3 = new ENDIFViewHolder(view);
Holders.add(v3);
return v3;
case item_nature.CALENDAR:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.calendar_layout, parent, false);
CALENDARViewHolder v4 = new CALENDARViewHolder(view);
Holders.add(v4);
return v4;
case item_nature.LOCATION:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.location_layout, parent, false);
LOCATIONViewHolder v5 = new LOCATIONViewHolder(view);
Holders.add(v5);
return v5;
}
return null;
}