A sample Android Studio Project showing using a Recyclerview as a Table View.
Here's the Recyclerview xml in the main layout.
<!-- Table View-->
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewMovieList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
tools:listitem="@layout/table_list_item" />
</HorizontalScrollView>
Here's the Recyclerview Adapter onBinding (all other stuffs are the same)
@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.table_header_cell_bg);
rowViewHolder.txtMovieName.setBackgroundResource(R.drawable.table_header_cell_bg);
rowViewHolder.txtYear.setBackgroundResource(R.drawable.table_header_cell_bg);
rowViewHolder.txtCost.setBackgroundResource(R.drawable.table_header_cell_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.table_content_cell_bg);
rowViewHolder.txtMovieName.setBackgroundResource(R.drawable.table_content_cell_bg);
rowViewHolder.txtYear.setBackgroundResource(R.drawable.table_content_cell_bg);
rowViewHolder.txtCost.setBackgroundResource(R.drawable.table_content_cell_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
}
While searching for TableView plugins, I found out that many of the Tableviews are really hard to setup and customize. So had to make one using Recyclerview, for adjusting the row height based on the content text.