3

I would like to know is there have anyway to have autosize/stretch/autofit in RecyclerView like TableLayout?

I have used TableLayout to present the data from database, but it's just too slow, took around 3+ seconds to show up the data.

And I have replace that with RecyclerView, it can show the data immediately, but it do not offer the stretchColumns options, and all text are squeezed.

Currently I have retrieve the maximum length of field from database, and calculate the possible width. But this solution is not good enough, some column are too wide for the data. Here is my code:

Layout for Recycler View

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
    android:fillViewport="true">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true" >

        <android.support.v7.widget.RecyclerView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/data_recycler"/>
    </HorizontalScrollView>
</ScrollView>

Layout for row

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="horizontal"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/col1"
        android:textSize="18sp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/col2"
        android:textSize="18sp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"/>
</LinearLayout>

ViewHolder

private class ViewItem extends RecyclerView.ViewHolder {
    private TextView col1;
    private TextView col2;

    ViewItem(View view) {
        super(view);

        col1 = (TextView) view.findViewById(R.id.col1);
        col1.setWidth(GetTextWidth(col1, columnWidth.get(0)));
        col2 = (TextView) view.findViewById(R.id.col2);
        col2.setWidth(GetTextWidth(col2, columnWidth.get(1)));
    }
}

private static int GetTextWidth(TextView textView, long stringLength) {
    String str = new String(new char[Integer.parseInt(stringLength + "")]).replace("\0", "W");
    return Math.round(textView.getPaint().measureText(str));
}
Prisoner
  • 1,839
  • 2
  • 22
  • 38
  • did you find any solution for this? – Joseph K. Jun 07 '17 at 08:42
  • https://stackoverflow.com/questions/44286207/tablelayout-with-recyclerview/44402928 – Joseph K. Jun 07 '17 at 08:42
  • @JosephK. Nope, at the end I have changed it to another layout – Prisoner Jun 08 '17 at 01:16
  • Which layout is it? Layout does not matter for me. Just want to create a scrollable table view which auto sizing columns. Thanks for your helps. – Joseph K. Jun 08 '17 at 06:52
  • @JosephK. I'm sorry I forgot the details, just whole layout changed. BTW, for your question, you may try to calculate the longest string and set the column width with that (as stated in my question, not good, but useable) – Prisoner Jun 08 '17 at 08:18

1 Answers1

0

If I correctly understood your question and you want do display items in several columns/rows with different sizes, you have to set LayoutManger in your case StaggeredGridLayoutManager for your RecyclerView.

int orientation = StaggeredGridLayoutManager.VERTICAL;
int spanCount = 2;

for vertical orientation spanCount is number of columns, otherwise if orientation is horizontal, spanCount is number of rows.

StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(spanCount, orientation);
recyclerView.setLayoutManager(gridLayoutManager);
Alex Kamenkov
  • 891
  • 1
  • 6
  • 16
  • Thanks, do I need to have layout xml for row? Currently I using `LinearLayoutManager`. Moreover, `StaggeredGridLayoutManager` can have same effect as `stretchColumns` in table layout? – Prisoner Oct 25 '16 at 03:25
  • Here is a pretty simple [tutorial](https://inducesmile.com/android/android-staggeredgridlayoutmanager-example-tutorial/) that will be helpful. – Alex Kamenkov Oct 25 '16 at 03:38
  • Thanks for the tutorial, as other tutorial I found, normally `StaggeredGridLayout` use for display some "island". I haven't test this layout yet, but I think the result should be something like [this](http://stackoverflow.com/a/34251368/1050927). However, I want to have something like datagrid, is there something I have missed? – Prisoner Oct 25 '16 at 03:47