将GridView添加到Android中的ListView

gre*_*eve 16 android listview gridview

我正在尝试创建一个ListView,它将包含两种类型的元素:字符串和GridView.
即将两个字符串和一个GridView放在一个ListView中.

布局应如下所示:

  • 字符串项1.1
  • 字符串项1.2
  • 字符串项1.3
  • 字符串项1.4
  • GridView项目1 GridView项目2
    GridView项目3 GridView项目4
  • 字符串项2.1
  • 字符串项目2.2
  • 字符串项2.3
  • 字符串项2.4

有没有办法做到这一点?

到目前为止,我只能在GridView中显示第一个项目,它在ListView中就像一个常规的String元素一样.

代码可以在这里查看:

任何帮助表示赞赏:)

gre*_*eve 51

回答我自己的问题:

基于这个答案,我创建了这个非常有效的类:

public class NonScrollableGridView extends GridView {
    public NonScrollableGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // Do not use the highest two bits of Integer.MAX_VALUE because they are
        // reserved for the MeasureSpec mode
        int heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightSpec);
        getLayoutParams().height = getMeasuredHeight();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我欠你六块钱,这完美无缺!:) (8认同)
  • 这个任务的问题,如果网格有很多数据,都是同时绘制的,这个网格上没有回收问题,可能会出现内存......必须有办法......也许有回收视图? (3认同)
  • 这是一个非常古老的问题/答案.现在可以通过例如RecyclerView轻松解决.看看这里:http://blog.sqisland.com/2014/12/recyclerview-grid-with-header.html (2认同)