Closed RajeshDhalange closed 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 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.
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.
In your adapter's
onBindCellViewHolder
method you receive the row and column index. You can pass them to yourViewHolder
to do what you need.
Thank its working fine.
But one doubt, each time onBindCellViewHolder assigning click listener will cause performance overhead?
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.
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?
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);
}
Thanks again It's working
Thank for this awesome work. I have two buttons in each cell, how can get to know which button was clicked?