gpt4 book ai didi

android - RecyclerView 自动大小列

转载 作者:太空狗 更新时间:2023-10-29 14:45:58 24 4
gpt4 key购买 nike

我想知道 RecyclerView 中是否有自动调整/拉伸(stretch)/自动调整功能?喜欢TableLayout ?

我用过TableLayout从数据库中呈现数据,但速度太慢,需要大约 3 秒以上才能显示数据。

我已将其替换为 RecyclerView , 它可以立即显示数据,但不提供 stretchColumns选项,并且所有文本都被压缩。

目前我已经从数据库中检索字段的最大长度,并计算出可能的宽度。但是这个解决方案不够好,有些列对于数据来说太宽了。这是我的代码:

回收站 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>

行布局

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

最佳答案

如果我正确理解了您的问题,并且您希望在不同大小的多个列/行中显示项目,则必须在您的案例 StaggeredGridLayoutManager 中为您的 RecyclerView 设置 LayoutManger。

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

对于垂直方向,spanCount 是列数,否则如果方向是水平的,spanCount 是行数。

StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(spanCount, orientation);
recyclerView.setLayoutManager(gridLayoutManager);

关于android - RecyclerView 自动大小列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40229951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com