简体   繁体   English

将GridView添加到Android中的ListView

[英]Add a GridView to a ListView in Android

I'm trying to create a ListView that will consist of two types of elements: Strings and a GridView. 我正在尝试创建一个将由两种类型的元素组成的ListView:字符串和GridView。
Ie putting both Strings and a GridView inside one single ListView. 即将字符串和GridView都放在一个ListView中。

The layout should look something like this: 布局应如下所示:

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

Is there any way to do this? 有什么办法吗?

As per now I can only show the first item in the GridView, and it acts just as a regular String element in the ListView. 到目前为止,我只能在GridView中显示第一项,并且它只是ListView中的常规String元素。

The code can be viewed here: 可以在这里查看代码:

Any help is appreciated :) 任何帮助表示赞赏:)

To answer my own question: 要回答我自己的问题:

Based on this answer I created this class that works very well: 基于此答案,我创建了非常有效的此类:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM