A sample Android Studio Project showing using a Recyclerview as a Table View.
TableView using Recyclerview (Android)
A sample Android Studio Project showing using a Recyclerview as a Table View.How the Height of Row works
The Height of row is determined by the cell, which has maximum multiline content. In this sample that cell is given wrapcontent and other cells are given matchparent. I've got to find a way to dynamically check this later.Upgraded to Kotlin (38% code reduction :))
(older java version can be found here
Here's the preview
Code Snippets
Here's the Recyclerview xml in the main layout.
Here's the Recyclerview Adapter onBinding (all other stuffs are the same)
Kotlin Version
val rowPos = holder.adapterPositionif (rowPos == 0) { // Header Cells. Main Headings appear here holder.itemView.apply { setHeaderBg(txtMovieName) txtMovieName.text = "Name" } } else { val modal = movieList[rowPos - 1]
holder.itemView.apply { setContentBg(txtMovieName) txtMovieName.text = modal.movieName } }
Click to see the example in Java.
@Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { RowViewHolder rowViewHolder = (RowViewHolder) holder;
int rowPos = rowViewHolder.getAdapterPosition();
if (rowPos == 0) { // Header Cells. Main Headings appear here rowViewHolder.txtRank.setBackgroundResource(R.drawable.tableheadercell_bg); rowViewHolder.txtMovieName.setBackgroundResource(R.drawable.tableheadercell_bg); rowViewHolder.txtYear.setBackgroundResource(R.drawable.tableheadercell_bg); rowViewHolder.txtCost.setBackgroundResource(R.drawable.tableheadercell_bg);
rowViewHolder.txtRank.setText("Rank"); rowViewHolder.txtMovieName.setText("Name"); rowViewHolder.txtYear.setText("Year"); rowViewHolder.txtCost.setText("Budget (in Millions)"); } else { MovieModal modal = movieList.get(rowPos-1);
// Content Cells. Content appear here rowViewHolder.txtRank.setBackgroundResource(R.drawable.tablecontentcell_bg); rowViewHolder.txtMovieName.setBackgroundResource(R.drawable.tablecontentcell_bg); rowViewHolder.txtYear.setBackgroundResource(R.drawable.tablecontentcell_bg); rowViewHolder.txtCost.setBackgroundResource(R.drawable.tablecontentcell_bg);
rowViewHolder.txtRank.setText(modal.getRank()+""); rowViewHolder.txtMovieName.setText(modal.getMovieName()); rowViewHolder.txtYear.setText(modal.getYear()+""); rowViewHolder.txtCost.setText(modal.getBudgetInMillions()+""); } }
@Override public int getItemCount() { return movieList.size()+1; // one more to add header row }