evrencoskun / TableView

TableView is a powerful Android library for displaying complex data structures and rendering tabular data composed of rows, columns and cells.
MIT License
3.14k stars 459 forks source link

Set custom background color for column header and row header #281

Closed selvarajmca18 closed 4 years ago

selvarajmca18 commented 4 years ago

Unable to set the custom background color for column header and row header. Also there is no attribute to disable/disallow selecting rows and columns.

evrencoskun commented 4 years ago

Hi @selvarajmca18,

You can disable displaying the shadow color using the below line; tableView.getSelectionHandler().setShadowEnabled(false)

To change the background color of the whole Column Headers, you can override the setSelection function like this.

public class ColumnHeaderViewHolder extends AbstractViewHolder {
..
@Override
    public void setSelected(@NonNull SelectionState selectionState) {
        super.setSelected(selectionState);

        if(!isSelected() && !isShadowed()){
            // Set your desired color
            setBackgroundColor(Color.CYAN);
        }
    }
}

gif1

If you want to change the background color of a specific Column Header, You need to follow these steps. I have already explained before for cell. You do it with the same approach for Column Headers.

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 AbstractViewHolderclass.

Thanks to setSelected method, It can be controlled considering to its selection state. So, even if TableView wants to set the default background color, it'll show yours the desired color.

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;

69_issue

selvarajmca18 commented 4 years ago

Okay @evrencoskun thanks for the info!..