在Android中将GridView添加到ListView [英] Add a GridView to a ListView in Android

查看:30
本文介绍了在Android中将GridView添加到ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a ListView that will consist of two types of elements: Strings and a GridView.
I.e. putting both Strings and a GridView inside one single 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

有没有办法做到这一点?

Is there any way to do this?

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

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.

代码可以在这里查看:

感谢任何帮助:)

推荐答案

回答我自己的问题:

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

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

这篇关于在Android中将GridView添加到ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆