last day (14 days later) » 

12:29 PM
0
Q: Zebra-striping color-effect on RecycledView widget rows

NactusWhat I'm trying to do is to assign a different color to every other row in a RecycledView. I created a custom RecycledView.Adapter, and in onCreateViewHolder method I have this: // Create a row_folder view View view = inflater.inflate(R.layout.row_folder, parent, false); if(position % 2 == 0) ...

 
Thanks for your comment. I'm not sure where getView goes in this context. Can it be implemented in the custom adapter ? Can you give me an example? Thanks.
 
getView is a method of Adapter. Even if you're using custom Adapter, it will be there. Beside, it is the method where you actually inject data in the view. Btw, are you able to solve it?
 
@Tushar, after looking at this: developer.android.com/reference/android/support/v7/widget/… I'm afraid the getView option won't work. It seems like RecyclerView.Adapter does not have a getView method. Any other suggestions ?
 
It has getItemViewType instead. Add that if condition in it.
 
Ok, so I implemented the getItemViewType instead, this method seems to get called for every row, but since it only takes a parameter of type int position, how do I reference the view I want to change the color for ?
 
12:29 PM
Kinda like this or even better way of doing is in onBindViewHolder.
 
Yah, using the onBindViewHolder(MyViewHolder holder, int position) was my initial thought, but I can't seem to be able to either extract (from the holder) or target the current view to access the "setBackgroundColor()" method
 
But it should work, show your code.
 
the problem I'm having is accessing the view in the holder, so ... something like this:
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

FolderRow thisRow = data.get(position);
holder.familyName.setText(thisRow.familyName);
holder.numOfCards.setText(thisRow.numOfCards);

if(position % 2 == 0) {
holder.<some_view_here>.setBackgroundColor(<some_color>);
}


}
 
where are you using this: View view = inflater.inflate(R.layout.row_folder, parent, false);
 
onCreateViewHolder
 
12:55 PM
Have you tried putting it in onBindViewHolder?
you need to add the view in onBindViewHolder
it has to be rootView
 
1:18 PM
hey Tushar, I think I got it working, it was an xml property that was blocking my background color
thanks much
 

last day (14 days later) »