我想知道在RecyclerView
中是否有像TableLayout
那样的autosize/stretch/autofit?
我已经使用TableLayout
来显示数据库中的数据,但是太慢了,显示数据大约需要3+秒。
我已经用RecyclerView
替换了它,它可以立即显示数据,但是它没有提供stretchColumns
选项,所有文本都被压缩。
目前我已从数据库中检索字段的最大长度,并计算可能的宽度。但是这个解决方案不够好,有些列太宽,无法容纳数据。这是我的代码:
回收站视图布局
<?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>
行布局
<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>
维怀特
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));
}
最佳答案
如果我正确理解了您的问题,并且您希望在多个不同大小的列/行中显示项目,则必须在您的案例中为recyclerview设置layoutmanager staggeredridlayoutmanager。
int orientation = StaggeredGridLayoutManager.VERTICAL;
int spanCount = 2;
对于垂直方向,spancount是列数,否则如果方向是水平的,则spancount是行数。
StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(spanCount, orientation);
recyclerView.setLayoutManager(gridLayoutManager);