博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android动态设定GridView的高度,固定column,实现高度自适应
阅读量:3725 次
发布时间:2019-05-22

本文共 911 字,大约阅读时间需要 3 分钟。

动态设定GridView的高度,固定column,根据gridview中的item个数设定高度:

调用以下方法:

[java] 
  1. public static void setListViewHeightBasedOnChildren(GridView listView) {  
  2.     // 获取listview的adapter  
  3.        ListAdapter listAdapter = listView.getAdapter();  
  4.        if (listAdapter == null) {  
  5.            return;  
  6.        }  
  7.        // 固定列宽,有多少列  
  8.        int col = 4;// listView.getNumColumns();  
  9.        int totalHeight = 0;  
  10.        // i每次加4,相当于listAdapter.getCount()小于等于4时 循环一次,计算一次item的高度,  
  11.        // listAdapter.getCount()小于等于8时计算两次高度相加  
  12.        for (int i = 0; i < listAdapter.getCount(); i += col) {  
  13.         // 获取listview的每一个item  
  14.            View listItem = listAdapter.getView(i, null, listView);  
  15.            listItem.measure(00);  
  16.            // 获取item的高度和  
  17.            totalHeight += listItem.getMeasuredHeight();  
  18.        }  
  19.   
  20.        // 获取listview的布局参数  
  21.        ViewGroup.LayoutParams params = listView.getLayoutParams();  
  22.        // 设置高度  
  23.        params.height = totalHeight;  
  24.        // 设置margin  
  25.        ((MarginLayoutParams) params).setMargins(10101010);  
  26.        // 设置参数  
  27.        listView.setLayoutParams(params);  
  28.    }  
调用此方法后,需要在调用notifyDataSetChanged()方法,实现界面刷新

转载地址:http://fopnn.baihongyu.com/

你可能感兴趣的文章
线程讲解(六)
查看>>
springBoot Thymeleaf 无法格式化日期总是默认的Wed Jun 23 21:59:37 CST 2021
查看>>
IDEA 2021 Spring Cloud 项目搭建 步骤演示 图文解说 (基础版)
查看>>
一个超神级框架——Hutool工具类应有尽有!
查看>>
推荐一个超级简单 Java 图形验证码模块
查看>>
Spring Cloud 五大组件总结
查看>>
推荐一款IDEA 快捷键 自动提示插件
查看>>
SpringCloud & SpringCloud Alibaba 整合
查看>>
SpringBoot 的.yml配置文件通用模板
查看>>
IDEA 2021 开发 springboot springcloud springcloud Alibaba应用时application.yml配置自动提示
查看>>
数据结构 字符串(三)前缀中的周期
查看>>
合同法律风险管理 合同理念(三)预防与救济并重
查看>>
Python爬虫 requests库应用详解
查看>>
Python爬虫 json库应用详解
查看>>
数据结构 二叉树(三)二叉树的深度
查看>>
数据结构 图(一)丛林中的路
查看>>
网络安全之HTTP协议
查看>>
计算机网络 网络概述
查看>>
合同法律风险管理 静态合同的主体
查看>>
合同法律风险管理 静态合同的序言与内容
查看>>