Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zebra view #106

Closed
joegillian opened this issue May 10, 2018 · 7 comments
Closed

Zebra view #106

joegillian opened this issue May 10, 2018 · 7 comments

Comments

@joegillian
Copy link

Hi,

Thanks for this great library.

Is it possible you to add zebra view feature, so that odd/even rows or columns have different background colors?

I managed to do it by checking cells' positions and setting their background colors. At first it seemed ok but if I sort the rows everything becomes messed up because row's background does not get updated by their new order.
Thanks again.

@evrencoskun
Copy link
Owner

evrencoskun commented May 10, 2018

Hi @joegillian

You can do whatever you want to do actually. Please check the #69 issue.

I managed to do it by checking cells' positions and setting their background colours. At first, it seemed ok but if I sort the rows everything becomes messed up because row's background does not get updated by their new order.

Could you please share your screen as gif maybe?

Thanks

@sonique6784
Copy link
Contributor

sonique6784 commented May 11, 2018

@joegillian ,
make sure you set the background properly at EACH onBindCellViewHolder and for BOTH case (even/odd).
you want to test with if the YPosition is odd or even , then apply the background accordingly.

if(YPosition is odd {
set colorA // handle both cases
} else {
set colorB // handle both cases
}

don't forget you are using a RecycleView, so cell might be recycle and come to you with parameter (like background), you are not expecting. so you have to style up everything again to make sure it match current context.

@joegillian
Copy link
Author

hi again,

i did it as you mentioned on issue #69 but still same. Could it be that i set the background on wrong place? i do it in the onBindCellViewHolder.

zebra

@evrencoskun
Copy link
Owner

Hi @joegillian,

You need to update each of the background value of cell item.

Assume that you have an tableView like this.

1.cellA : blue
2.cellB : white
3.cellC : blue

after sorting process, each cell item shows its own background color.

1.cellC: blue
2.cellA: blue
3.cellB: white

So, what is the solution? You just need to update each of cell item after the sorting process.

just update your cell model data with right color and run the notifyDataSetChanged function of the TableViewAdapter

@joegillian
Copy link
Author

joegillian commented May 12, 2018

Thanks, I did it as you say and it works perfect.

@evrencoskun
Copy link
Owner

Great!

I think, the easiest way setting background color at binding process.

@Override
     public void onBindCellViewHolder(AbstractViewHolder holder, Object cellItemModel, int 
             columnPosition, int rowPosition) {
          Cell cell = (Cell) cellItemModel;
          
          // Determine the right color considering to the row position;
          int bgColor = rowPosition % 2 == 0 ? myFirstColor : mySecondColor;
          
          // Set the background color to model
           cell.setBackgroundColor(bgColor);

           // Get the holder to update cell item value
           MyCellViewHolder viewHolder = (MyCellViewHolder) holder;
           viewHolder.setCell(cell);      
}

With this way, It will set the right color every time, not just for once. However, after sorting process, you need to call notifyDataSetChanged as well. Because, the cell items whose positions are unchanged after the sorting process, won't be notified.

@Aleksa-Smolovic
Copy link

I tried both this solution and the one at 69, it didn't work, also I have to change bg color of cell like this
app:unselected_color="@color/grayBlack", it doesn't work when I change it in cell layout. What to do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants