Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在(扩展的)AdapterView中滚动时添加子视图_Android_Android Widget_Android Adapterview - Fatal编程技术网

Android 在(扩展的)AdapterView中滚动时添加子视图

Android 在(扩展的)AdapterView中滚动时添加子视图,android,android-widget,android-adapterview,Android,Android Widget,Android Adapterview,我正在创建一个包含许多子视图(例如按钮)的滚动面板。每个子视图都有一个基于其行和列索引的固定位置。我不能在一开始就创建它们,因为它们有很多,而且我的内存会用完,所以我只想在与屏幕视图端口相交时添加一个子视图(当用户滚动到该区域时)。我使用如下内容覆盖onLayout()方法: @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l,

我正在创建一个包含许多子视图(例如按钮)的滚动面板。每个子视图都有一个基于其行和列索引的固定位置。我不能在一开始就创建它们,因为它们有很多,而且我的内存会用完,所以我只想在与屏幕视图端口相交时添加一个子视图(当用户滚动到该区域时)。我使用如下内容覆盖onLayout()方法:

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    for (int row = 0; row < ROW_NUM; row++) {
        ColumnAdapter columnAdapter = mRowAdapter.getItem(row);
        for (int col = 0; col < COLUMN_NUM; col++) {
            ItemView itemView = columnAdapter.getView(col, null, this);
            if (isOnScreen(row, col)) {
                itemView.layout(col * 100, row * 100, (col + 1) * 100, (row + 1) * 100);
                addViewInLayout(itemView, row + col, null, true);
            }
        }
    }
    scrollTo(getScrollX(), getScrollY());
}
@覆盖
仅受保护的void布局(布尔值已更改、int l、int t、int r、int b){
超级在线布局(更改,l,t,r,b);
对于(int row=0;row

(ColumnAdapter是适配器扩展,ItemView是按钮扩展)。这不起作用,因为在滚动期间不会调用onLayout()。我应该怎么做才能在用户滚动时动态添加ItemView< p>你也可以考虑使用ListVIEW。它做的正是您想要的,这是它的元素的惰性负载。有关详细信息,请参阅上的Google IO会话

简而言之,您正在扩展的适配器类有一个
View-getView(int-position,View-convertView,ViewGroup-parent)
函数。可以重写此函数,并检查convertView是否为null,并加载它,因为它实际上是该子元素以前创建的视图


另外还有一些关于避免findViewById()函数调用的详细信息,因为它很昂贵。

Hi Noel,感谢您的回复。我很想使用ListView,但我正在创建的小部件包含许多可以一起滚动的列,而ListView单独处理它们自己的滚动。