【问题标题】:Add a GridView to a ListView in Android在 Android 中将 GridView 添加到 ListView
【发布时间】:2011-04-21 05:00:41
【问题描述】:

我正在尝试创建一个包含两种类型元素的 ListView:Strings 和 GridView。
IE。将 Strings 和 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 中的常规字符串元素一样。

代码可以看这里:

任何帮助表示赞赏:)

【问题讨论】:

    标签: android gridview listview


    【解决方案1】:

    回答我自己的问题:

    基于this answer我创建了这个运行良好的类:

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

    【讨论】:

    • 这个任务的问题,如果网格有很多数据,所有的都是同时绘制的,这个网格没有回收问题,可能会内存不足...一定有办法......也许有回收视图?
    • 这是一个非常古老的问题/答案。它可以很容易地解决,例如回收站现在查看。例如看这里:blog.sqisland.com/2014/12/recyclerview-grid-with-header.html
    • @greve 你成就了我的一天。这种方法非常有效
    • 谢谢伙计!我的问题通常不会这么容易解决!
    • 完美答案。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    相关资源
    最近更新 更多