【问题标题】:Android custom component - How to automatically fix the width of columnsAndroid 自定义组件 - 如何自动修复列宽
【发布时间】:2014-08-13 19:39:32
【问题描述】:

我创建了一个名为 ScrollListView 的自定义组件,它基本上是一个表格,扩展了 ListView。使用下面的函数 getView,我用来自数据库的数据填充这个组件。它完美地工作,结果在视觉上是:

显然我希望单元格对齐,如下所示:

为了实现这一点,目前我执行计算测量所有单元格大小,然后根据最宽单元格调整列,但多次调用GC,导致滚动时出现延迟,如之前报道的is this thread

我的问题是:如何自动修复/测量列的大小,而不在每次 getView 调用中调用疯狂的计算,消除所有这些 GC 事件?我试图扩展 GridView 而不是 ListView,但它不起作用。我是 Android 新手。

(我不能使用标准组件,例如 GridView 或 GridLayout,我必须使用我的自定义组件 ScrollListView,因为其他更复杂的功能目前可以正常运行)

在此先感谢,抱歉我的英语不好

编辑:这是我的代码(getView 代码在已经提到的线程中):

scroll_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:background="@color/BackGroundColor"
    android:scrollbars="none" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:gravity="center_horizontal">

    <TableLayout
        android:id="@+id/header_lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal">
    </TableLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <android.ListView
             android:id="@+id/rows_lv"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_margin="0dip"
             android:divider="@null"
             android:dividerHeight="0dp"
             android:padding="0dip"
             android:scrollbars="horizontal"
             android:gravity="center_horizontal"
             android:stretchColumns="*"
             />
    </LinearLayout>

    </LinearLayout>

</HorizontalScrollView>

还有我的 ScrollListView.java

  public class ScrollListView extends LinearLayout
{

    TableLayout header;
    ListView rows;
    public ScrollListView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view=layoutInflater.inflate(R.layout.scroll_listview,this);

        if (!this.isInEditMode())
        {
            rows = (ListView)this.findViewById(R.id.rows_lv);
            header = (TableLayout)this.findViewById(R.id.header_lv);
            rows.setOnRedrawListener(new OnListViewRedraw() {


                @Override
                public void onBeforeRedraw() {

                }

                @Override
                public void onAfterRedraw() {
                }

            });
            rows.setHeader(header);
            rows.setMyParent(this);
        }

    // (and so on...)

【问题讨论】:

  • 你能把你的xml文件贴在Listview每一行的膨胀视图上吗?
  • 你好 Maxouille,我更新了我的问题
  • 我将在您的其他帖子中发布我的答案,因为它更多地涉及如何提高您在 getView 方法中的性能

标签: android listview gridview garbage-collection adapter


【解决方案1】:

查看TableLayout.onMeasure() 源代码。他们找到所有孩子的单元格宽度,然后调整所有孩子以相应地匹配。您将需要一些修改版本来满足您的需求。

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 2015-04-29
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多