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));
}