Comments (6)
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;
}
from tableview.
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!
from tableview.
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?
from tableview.
I have problem with Zebra pattern with my table, its not working.
Please provide source code of zebra Pattern.
Thanks..
from tableview.
Hi @poojamantri93 please check #106
from tableview.
@evrencoskun i want to add propertychangelistner for each cell that should be called everytime when the value of the cell gets changed
from tableview.
Related Issues (20)
- A problem HOT 1
- addpropertychangelistener() for each cell.when the cell value changes it should be triggered
- java.lang.IndexOutOfBoundsException in setRowHeaderItems HOT 1
- Help HOT 1
- Set a default void implementation on Cell, HeaderCell, RowCell event listeners
- onCellClicked returns correct cellView but incorrect row after sorting HOT 4
- cellviewholder with edit Text is not working after scroll
- How to color the whole row when selecting a cell??
- Column header not match cell when to reload HOT 1
- Forcing sticky column header
- Wrong number of cells in a row crashes
- setColumnWidth function is buggy when implemented in dialog fragment
- I wonder how to exchange rows and columns
- Implement performClick in cellRecyclerView and etc
- Multiline text in cells HOT 1
- how to filter in rowheader / cornerview
- while building -Failed to resolve: com.evrencoskun.library:tableview:0.8.9.4 HOT 2
- Help needed for configurating in build.gradle.kts
- The column width follows the adapter that was first opened
- add(Cell) causing mis alignment align
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tableview.