Creating a Table view in Android using Recyclerview

While working on an Android App, there came a requirement to add Table View. I’ve tried several Table View Libraries, but found them too confusing to setup. And when I got a case where the text needs to be fit in, and rows needs to be in equal height when the text grows big, I’ve to rely on RecyclerView to implement my own TableView. So Here it goes.

RecyclerView

Here’s our Recyclerview List Item. The layout height is set to wrap_content for the
maximum content cell, and all others are set to match_parent so when that cell
gets expanded, all other cells also expands.

Table View we are making is a RecyclerView, with a row Item populated. To make the row expanding based on the height of the content of the cell which has the biggest content (height).

Setting up Header And Cells in Adapter

Header has different BG and Color.

In order to setup the cells and header, you need to make some modification in the adapter.
  1. Add a null value as the first item, so that header will be counted.
  2. If Position is zero, setup the viewHolder with the Title String values and set a different background drawable to make it stand out.
  3. If position not zero, fill it with content values, and change the drawable.

Making the Table Scrollable

To make the RecyclerView scrollable horizontally, you can put the RecyclerView inside a horizontal scrollview also by adding property 
<app:layout_behavior=”@string/appbar_scrolling_view_behavior”>

Also when adding to the fragment, adding to the recyclerView the property android:nestedScrollingEnabled=”true”

I’m Planning to make a simple example with GIF Animations, and host it to GitHub. Comments are welcome on any issues that arises.

190 Total Views 1 Views Today

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *