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

How to make specific cell layout react to click? #131

Closed bogdanmunteanu closed 6 years ago

bogdanmunteanu commented 6 years ago

Hi @evrencoskun,

I'm trying to show a selection state on TableView by displaying a green tick when the user clicks a specific cell.

Where I should put the code to make this happen ? From the table listener i cannot access the layout only the cell data.

     @Override
public void onCellClicked(@NonNull RecyclerView.ViewHolder cellView, int column, int row) {
    Log.e("Order table listener::::" ,mTableView.getAdapter().getCellItem(column,row).toString());
    Cell selectedCell =(Cell) mTableView.getAdapter().getCellItem(column,row);
   }

This works ok , as it displays the correct data. I've tried also from the adapter but the click listener is not set. (This seems like the correct approach)

    @Override
public void onBindCellViewHolder(AbstractViewHolder holder, Object cellItemModel, int
        columnPosition, int rowPosition) {
    Cell cell = (Cell) cellItemModel;

        // Get the holder to update cell item text
        CellViewHolder viewHolder = (CellViewHolder) holder;
        viewHolder.setDate(cell.getDate());
        viewHolder.cell_container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("Selected cell :::",cell.toString());
                if(cell.isSelected())
                {
                    cell.setSelected(false);
                    viewHolder.selection.setVisibility(View.GONE);
                }else
                {

                        cell.setSelected(true);
                        viewHolder.selection.setVisibility(View.VISIBLE);

                }
            }
        });

}

Is there a way of achieving this ?

evrencoskun commented 6 years ago

Hi @bogdanmunteanu

The first parameter is the clicked cell view holder. So, you can get what you want using TableViewListener.

@Override
public void onCellClicked(@NonNull RecyclerView.ViewHolder cellView, int column, int row) {
      // if you have just one view type, you can directly cast your class.    
      if (cellView instanceof CellViewHolder) {
            CellViewHolder cellViewHolder = (CellViewHolder) cellView;
      }
     ....
   }
bogdanmunteanu commented 6 years ago

Hi @evrencoskun ,

Yes, i knew that but the problem was that I did not how to access the layout . I did the following

  Log.e("Order table listener::::" ,mTableView.getAdapter().getCellItem(column,row).toString());
    Cell selectedCell =(Cell) mTableView.getAdapter().getCellItem(column,row);
  CellViewHolder holder = (CellViewHolder)    mTableView.getCellLayoutManager().getCellViewHolder(column,row);
      if(holder!=null) {
          holder.selectionImage.setVisibility(View.VISIBLE);
   }

Also , I used butterknife in the holders because with findViewById in ocasionally throwed some NPEs.

@BindView(R.id.cell_container)
public LinearLayout cell;

@BindView(R.id.cell_data)
public TextView cellText;

@BindView(R.id.selectionCheck)
public ImageView selectionImage;

@BindView(R.id.ivNotAvailable)
public ImageView notAvailable;
 public CellViewHolder(View itemView) {
     super(itemView);
    cell_textview = (TextView) itemView.findViewById(R.id.cell_data);
    cell_container = (LinearLayout) itemView.findViewById(R.id.cell_container);
    ButterKnife.bind(this,itemView);
 }

All good and working. Thanks for your support , issue can be closed :+1: :+1: