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

Listen click on specific view on multi view cell #296

Closed RajeshDhalange closed 4 years ago

RajeshDhalange commented 4 years ago

Thank for this awesome work. I have two buttons in each cell, how can get to know which button was clicked?

MGaetan89 commented 4 years ago

How about setting the click listener on your button directly when you create the ViewHolder of your cell, instead of using the global cell click listener?

RajeshDhalange commented 4 years ago

How about setting the click listener on your button directly when you create the ViewHolder of your cell, instead of using the global cell click listener?

How can I access column and row position in ViewHolder? not found any way there.

MGaetan89 commented 4 years ago

In your adapter's onBindCellViewHolder method you receive the row and column index. You can pass them to your your ViewHolder to do what you need.

https://github.com/evrencoskun/TableView/blob/c33f96fa09f3e355e4bdcaafec468db46e6b9523/tableview/src/main/java/com/evrencoskun/tableview/adapter/ITableAdapter.java#L46

RajeshDhalange commented 4 years ago

In your adapter's onBindCellViewHolder method you receive the row and column index. You can pass them to your ViewHolder to do what you need.

https://github.com/evrencoskun/TableView/blob/c33f96fa09f3e355e4bdcaafec468db46e6b9523/tableview/src/main/java/com/evrencoskun/tableview/adapter/ITableAdapter.java#L46

Thank its working fine.
But one doubt, each time onBindCellViewHolder assigning click listener will cause performance overhead?

MGaetan89 commented 4 years ago

I would set it in your ViewHolder constructor, it that's possible. Don't recreate a new click listener each time the cell is updated. And you can keep the row and column index in your ViewHolder, so they can be accessed from your click listener.

RajeshDhalange commented 4 years ago

ViewHolder

That is the issue I don't find any method to access column and row position in ViewHolder constructor. Can you please provide a sample code for access column and row position in ViewHolder constructor?

MGaetan89 commented 4 years ago

It's not in the constructor of the ViewHolder, but it's coming from the onBindCellViewHolder() method. Here's a sample code (I quickly wrote it here, so it might need some adaptation):

// Adapt your current ViewHolder to have something like this
public class CellViewHolder extends AbstractViewHolder {
    private int column = -1;
    private int row = -1;

    public CellViewHolder(@NonNull View itemView) {
        super(itemView);

        itemView.setOnClickListener(v -> {
            if (column >= 0 && row >= 0) {
                // TODO Do something with column and row
            }
        });
    }

    public void bindTo(Cell cell, int column, int row) {
        this.column = column;
        this.row = row;

        // Your usual binding
    }
}

// Adapt your current onBindCellViewHolder to have something like this
public void onBindCellViewHolder(@NonNull AbstractViewHolder holder, @Nullable Cell cellItemModel, int columnPosition, int rowPosition) {
    ((CellViewHolder) holder).bindTo(cellItemModel, columnPosition, rowPosition);
}
RajeshDhalange commented 4 years ago

Thanks again It's working