-1

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;
}
ANS Hmida
  • 11
  • 3
  • Here https://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type – coroutineDispatcher Apr 16 '19 at 16:35
  • That does not answer my question. – ANS Hmida Apr 16 '19 at 16:40
  • 1
    You don't understand the basics of `RecyclerView` - `ViewHolders` are only created when needed and re-used (recycled) for different items as You scroll. You should use `Adapter.notifyItemChanged(position)` and reflect color change within `Adapter.onBindViewHolder(holder, position)`. – Pawel Apr 16 '19 at 16:50

1 Answers1

0

Do not access or modify Viewholder's views outside of onBindViewHolder. Instead, u can use associational data with view.

Think about your adapter class have global variable type of HashMap colorMap . The HashMap's key will be index and value will be color .

For example you want to change second row color to red.

colorMap.put(2, Color.RED);
adapter.notifyItemChanged(2); // it can be notifyDataSetChanged(); for the other view need to be change.

Finally your notifyItemChanged or notifyDataSetChanged method call cause of rebind view .

@Override
public void onBindViewHolder(@NonNull ABCHolder abcHolder, int i) {
       if(colorMap.containsKey(abcHolder.getAdapterPosition())
          abcHolder.itemView.setBackgroundColor(a.get(abcHolder.getAdapterPosition()));

Unlike this approach your view modify strategy be taken back when onBindViewHolder recalling.

Gökberk Yağcı
  • 376
  • 1
  • 4
  • 10