47

There is only two layout manager for RecyclerView: LinearLayoutManager and GridLayoutManager. Is there a way to use TableLayout as layout manager with RecyclerView? Any suggestion?

EDIT: I wonder if there is any solution without writing a layout manager from zero. TableLayout behaviour is enough for me, just want to add recycle feature for performance issues on handling large amount fo data.

And creating a table looking listview is not solve my problem I think, because my table is very dynamic, I don't know even column names, customer deciding all details of the table. So cells would be custom, column widths needs to be auto resizing depend on content length. I don't think listview can handle that.

Thanks in advance for helps.

Joseph K.
  • 784
  • 1
  • 9
  • 19
  • what is your requirements? – Jaydeep Devda May 31 '17 at 13:32
  • 1
    http://cases.azoft.com/create-custom-layoutmanager-android/ – Pavya May 31 '17 at 13:36
  • I need to implement a dynamic table view which will be use large amount of data. So I think it must be used somehow RecyclerView to performance issues. – Joseph K. May 31 '17 at 13:36
  • can you show, what is your actual requirements than we can help you out with more ease. – Amit Vaghela Jun 02 '17 at 13:51
  • @amit It is just a table view (like `JTable`in swing) which capable of handling large amount of data. I have to show custom components like combobox, button in cells. And of course I will add some features like sorting, filltering etc. Actually I think I can add them to that features in `TableLayout` but `TableLayout` is not the best solutions for large data, because it needs to create all views before the showing on the screen. Of course I am not insisting on `TableLayout`, I am open for other solutions. – Joseph K. Jun 02 '17 at 14:01
  • you can use listview and design it row file as tableview and than load minimumn data like 100 records and provide a next button(or you can maintain that on scrolling down) to do that and according to that data will be displayed. it can also be very dynamic. you can comment if you have go though this approach and stuck somewhere@JosephK. – Amit Vaghela Jun 02 '17 at 14:36
  • 4
    If you have not seen it yet, also consider Google's new FlexboxLayout: https://android-developers.googleblog.com/2017/02/build-flexible-layouts-with.html – albert c braun Jun 02 '17 at 19:15
  • nice, I will check it out, thanks. – Joseph K. Jun 02 '17 at 20:54
  • The is also `StaggeredGridLayoutManager` in the support lib. Is that an option for your case ? – Tin Tran Jun 05 '17 at 15:20
  • The solution I know is using an auto-resizing GridLayoutManager. The number of items on a row spans to fill the screen width but the only drawback is the the width of each of the grids is constant--can't be resized. – X09 Jun 07 '17 at 12:48
  • If recycle view is not mandatory than you can use "addView"(Table Row) property to parent TableLayout. – ashish Jun 07 '17 at 13:08
  • @ashish could you explain more? `RecyclerView` is not mandatory but recycle mechanism is mandatory. Do you have any example to do it by `addView`. – Joseph K. Jun 08 '17 at 06:56
  • @Joseph please take a look : https://notepad.pw/wlerivp1 – ashish Jun 08 '17 at 07:03
  • You wrote everything but still your question is unclear. When you say recyclerview will give you performance issue what do you mean by this? Did you try it? what happened when you tried? and recyclerview doesn't create all the views before showing it. It's like a object pool it creates that much objects only which is visible to user and reuses it so I don't see any performance issue there may be you are doing something else while showing data like background calculation n all and do you know about pagination? https://medium.com/@etiennelawlor/pagination-with-recyclerview-1cb7e66a502b? please s – Reyansh Mishra Jun 07 '17 at 03:20
  • I mean memory issues actually. In table layout I need to create all rows initially but with recycle view (as you say) creating only a few views is enough. But there is no option to use recycleview as a table. It can be done by `LinearLayoutManaager` but my table is very dynamic, as I said above I did not even know the column names. So column widths should auto resized. I will try to do my question more clear. – Joseph K. Jun 07 '17 at 06:41
  • yup please form your question properly. – Reyansh Mishra Jun 07 '17 at 07:42

4 Answers4

43

There are many open source libraries and codes available to find out how it can be developed.

  1. https://github.com/evrencoskun/TableView
  2. https://github.com/HYY-yu/TableRecyclerView
  3. https://github.com/Cleveroad/AdaptiveTableLayout
  4. https://github.com/celerysoft/TableFixHeaders

TableView

ziLk
  • 3,120
  • 21
  • 45
12

Have a look at this library. Seems it's exactly what you need. It has a recycling system as well.

romtsn
  • 11,704
  • 2
  • 31
  • 49
  • It is good, I have already found it. Thanks for share. I am still looking its architecture and it seems there is no auto-size column feature. And also I think its code is too complex to maintain and add new features. – Joseph K. Jun 09 '17 at 11:47
  • @JosephK. well, I think you have a non-trivial question, that assumes some complex extra-ordinary coding. So it's expected that you should think a lot how to implement it :) I don't see any problems to fork the repo and adjust it for your needs. And yes you'll need to calculate layout size by yourself depending on your cell content, so supposedly you'll have something like `AutoSizeRelativeLayout`. – romtsn Jun 09 '17 at 13:45
  • Of course I totally understand that it is hard to implement a complex table in android. I am still looking at this and other github projects to find the best way to implement a table view. Thanks for `AutoSizeRelativeLayout` tip. – Joseph K. Jun 09 '17 at 13:52
  • btw, +1 for tips and guidance. – Joseph K. Jun 09 '17 at 13:52
3

I tried this for making a table like structure, and it worked.

For your RecyclerView's adapter implementation, create a Horizontalscrollview layout, and place all your elements inside it, in your adapter layout xml for each row.

In the main layout xml where you are adding the recycler view, add a HorizontalScrollView as its parent. Now, add a TableLayout to the HorizontalScrollView

I am adding below code for adapter layout xml:

<HorizontalScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/horizontalScroll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <!--Any custom layout here as per your need-->
</HorizontalScrollView>

I am adding below code for main layout xml:

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <HorizontalScrollView>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/empView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="select"
        android:scrollbars="vertical">        
    </android.support.v7.widget.RecyclerView>
        </HorizontalScrollView>

</TableLayout>

This will ensure that you are able to have a table like structure to scroll both horizontally and vertically using recyclerview.

The HorizontalscrollView enables you to horizontally scroll the entire list inside the TableLayout. The RecyclerView enables you to vertically scroll the list.

Hope this works for you.

Banana
  • 2,435
  • 7
  • 34
  • 60
koushick
  • 157
  • 1
  • 8
  • If I have objects like student for example will it have all of the students information at 1 student per line? – FernandoZ Aug 01 '23 at 05:44
-2

Create a custom row with linearlayout and than attach that row to recyclerView Adapter this is only the solution for making a table like Structure in RecyclerView.

petey
  • 16,914
  • 6
  • 65
  • 97
PArth SOni
  • 117
  • 1
  • 11