TableView
TableView copied to clipboard
Change Cell Text Color
Your work really perfect. It took a long time for understanding, but the result is excellent.
I have a problem with CellHolder. I want to change cell text color for some situations. But I could not. Where I can change cell text color in your sample project?
Thanks.
Hi @schlaincher,
If you need to change text color considering the selection state, there is a simple way that I can show you.
AbstractViewHolder
that is the superclass of your cell holder has a protected function that is setSelected
. You can just override it like this.
@Override
public void setSelected(SelectionState selectionState) {
super.setSelected(selectionState);
if (selectionState == SelectionState.SELECTED) {
cell_textview.setTextColor( mySelectedTextColor);
} else {
cell_textview.setTextColor(ContextCompat.getColor(cell_textview.getContext(), R.color
.unselected_text_color));
}
}
You can also check the sample app that has the same control.
Have a nice day!
Thanks. It works perfect. But now, I have a new question. How can I have zebra pattern with my table? Do you have a solution for this?
Hi @schlaincher @azisoli,
I reopened this issue. Becuase, both of you has the same need.
In your TableView, if Each cell item can have a different background color, then you need to create an instance for background color on your Cell Model class.
For example;
public class Cell {
private int mBackground = -1;
...
public void setBackgroundColor(int backgroundColor) {
mBackground = backgroundColor;
}
public int getBackgroundColor() {
return mBackground;
}
}
Each cell view holder needs to take its cell model on onBindCellViewHolder
of your TableViewAdapter.
@Override
public void onBindCellViewHolder(AbstractViewHolder holder, Object cellItemModel, int
columnPosition, int rowPosition) {
// Get the holder to update cell item
CellViewHolder viewHolder = (CellViewHolder) holder;
// bind the cell view holder
viewHolder.setCell(cell);
}
However, even if you change the color of the item, TableView also changes the color considering to Selection state. So How can we handle this situation?
public class CellViewHolder extends AbstractViewHolder {
public final TextView cell_textview;
public final LinearLayout cell_container;
private Cell cell;
public CellViewHolder(View itemView) {
super(itemView);
cell_textview = (TextView) itemView.findViewById(R.id.cell_data);
cell_container = (LinearLayout) itemView.findViewById(R.id.cell_container);
}
public void setCell(Cell cell) {
this.cell = cell;
// Change the text of the item
cell_textview.setText(String.valueOf(cell.getData()));
// If your TableView should have auto resize for cells & columns.
// Then you should consider the below lines. Otherwise, you can ignore them.
// It is necessary to remeasure itself.
cell_container.getLayoutParams().width = LinearLayout.LayoutParams.WRAP_CONTENT;
cell_textview.requestLayout();
}
@Override
public void setSelected(SelectionState p_nSelectionState) {
super.setSelected(p_nSelectionState);
if (p_nSelectionState != SelectionState.SELECTED) {
// Change the background color
setBackgroundColor(cell.getBackgroundColor());
}
}
}
As you can see the above code line we have an override method setSelected(SelectionState p_nSelectionState)
of the AbstractViewHolder
class.
Thanks to setSelected
method, It can be control considering to it selection state. So, even if TableView wants to set the default background color, you can set the desired color like below.
In my case, I set the color on long press action like this.
@Override
public void onCellLongPressed(@NonNull RecyclerView.ViewHolder cellView, final int column,
int row) {
// Get the cell item model
Cell cell = (Cell) mTableView.getAdapter().getCellItem(column, row);
// Change the color;
cell.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorAccent));
// Change the cell item model
mTableView.getAdapter().changeCellItem(column, row, cell);
}
And this is the result;
How can I have the zebra pattern on my table?
You can set any zebra pattern that you want using the below way.
For example; I have set the background color for my dummy list like this;
private List<List<Cell>> getSimpleCellList() {
int bgColor;
List<List<Cell>> list = new ArrayList<>();
for (int i = 0; i < ROW_SIZE; i++) {
List<Cell> cellList = new ArrayList<>();
// Use the zebra background color for even numbers
if (i % 2 == 0) {
bgColor = ContextCompat.getColor(mContext, R.color.cell_zebra_background_color);
} else {
bgColor = ContextCompat.getColor(mContext, R.color.cell_background_color);
}
for (int j = 0; j < COLUMN_SIZE; j++) {
String text = "cell " + j + " " + i;
if (j % 4 == 0 && i % 5 == 0) {
text = "large cell " + j + " " + i + ".";
}
String id = j + "-" + i;
Cell cell = new Cell(id, text, bgColor);
cellList.add(cell);
}
list.add(cellList);
}
return list;
}
I have problem with Zebra pattern with my table, its not working. Please provide source code of zebra Pattern.
Thanks..
Hi @poojamantri93 please check #106
@evrencoskun i want to add propertychangelistner for each cell that should be called everytime when the value of the cell gets changed