问题描述:

有时我们使用GridView会面对类似这种情况。

这是是不是一脸愣逼,我们理想情况是把他变成这样

保证同一行的item都是一样高这样就美观许多了

注意:上面的两张图片是盗图,用来作为效果观看的,自己在手机上截图比较麻烦(其实就是懒 哈哈~)

解决方案:

     第一种方式 (不推荐): 不怎么灵活,比较麻烦 ,自己改写adapter中的代码 参考这个:https://blog.csdn.net/zhi890701/article/details/50510822

    第二种方式(推荐):因为博主用的是通用的adapter自己改起来比较麻烦还要测试,所以就看看有没有重写GridView的,正好被博主找到了,很方便

    https://github.com/JJdeGroot/AutoGridView

    使用起来也方便里面就写了一个AutoGridView.java直接拷贝到项目中就可以和普通GridView一样使用了懒的话就直接拷贝我这代码就行了  代码:

import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.ListAdapter; /**
* Automatically calculates the ideal for each row
* @author JJ
*
*/
public class AutoGridView extends GridView { private static final String TAG = "AutoGridView";
private int numColumnsID;
private int previousFirstVisible;
private int numColumns = 1; public AutoGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
} public AutoGridView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
} public AutoGridView(Context context) {
super(context);
} /**
* Sets the numColumns based on the attributeset
*/
private void init(AttributeSet attrs) {
// Read numColumns out of the AttributeSet
int count = attrs.getAttributeCount();
if(count > 0) {
for(int i = 0; i < count; i++) {
String name = attrs.getAttributeName(i);
if(name != null && name.equals("numColumns")) {
// Update columns
this.numColumnsID = attrs.getAttributeResourceValue(i, 1);
updateColumns();
break;
}
}
}
Log.d(TAG, "numColumns set to: " + numColumns);
} /**
* Reads the amount of columns from the resource file and
* updates the "numColumns" variable
*/
private void updateColumns() {
this.numColumns = getContext().getResources().getInteger(numColumnsID);
} @Override
public void setNumColumns(int numColumns) {
this.numColumns = numColumns;
super.setNumColumns(numColumns); Log.d(TAG, "setSelection --> " + previousFirstVisible);
setSelection(previousFirstVisible);
} @Override
protected void onLayout(boolean changed, int leftPos, int topPos, int rightPos, int bottomPos) {
super.onLayout(changed, leftPos, topPos, rightPos, bottomPos);
setHeights();
} @Override
protected void onConfigurationChanged(Configuration newConfig) {
updateColumns();
setNumColumns(this.numColumns);
} @Override
protected void onScrollChanged(int newHorizontal, int newVertical, int oldHorizontal, int oldVertical) {
// Check if the first visible position has changed due to this scroll
int firstVisible = getFirstVisiblePosition();
if(previousFirstVisible != firstVisible) {
// Update position, and update heights
previousFirstVisible = firstVisible;
setHeights();
} super.onScrollChanged(newHorizontal, newVertical, oldHorizontal, oldVertical);
} /**
* Sets the height of each view in a row equal to the height of the tallest view in this row.
* @param firstVisible The first visible position (adapter order)
*/
private void setHeights() {
ListAdapter adapter = getAdapter(); if(adapter != null) {
for(int i = 0; i < getChildCount(); i+=numColumns) {
// Determine the maximum height for this row
int maxHeight = 0;
for(int j = i; j < i+numColumns; j++) {
View view = getChildAt(j);
if(view != null && view.getHeight() > maxHeight) {
maxHeight = view.getHeight();
}
}
//Log.d(TAG, "Max height for row #" + i/numColumns + ": " + maxHeight); // Set max height for each element in this row
if(maxHeight > 0) {
for(int j = i; j < i+numColumns; j++) {
View view = getChildAt(j);
if(view != null && view.getHeight() != maxHeight) {
view.setMinimumHeight(maxHeight);
}
}
}
}
}
}
}

还可以参考这个项目内容 https://github.com/etsy/AndroidStaggeredGrid 看介绍可以实现瀑布流哦  具体自行了解下

GridView解决同一行item的高度不一样,如何同一行统一高度问题?的更多相关文章

  1. Android ScrollView嵌套GridView导致GridView只显示一行item

    Android ScrollView嵌套GridView导致GridView只显示一行item Android ScrollView在嵌套GridView时候,会导致一个问题发生:GridView只显 ...

  2. CSS| 解决子级用css float浮动 而父级div没高度不能自适应高度

    解决子级用css float浮动 而父级div没高度不能自适应高度 解决子级对象使用css float浮动 而父级div不能自适应高度,不能被父级内容撑开解决方法,父级div没有高度解决方法. 最外层 ...

  3. 关于div容器在ie6下默认高度不为0(存在默认高度)

    最近做项目的时候遇到一个问题,相信很多人都遇到过,就是在测试兼容性的时候,在ie6下小于12px 的背景的高度不等于原高,或许这样说你可能不是很明白,那就举个例子吧! 如图所示: 锯齿状的背景图本来是 ...

  4. JS table新增一行的时候 如何在新增的这一行把样式也加进去 例如变成<tr class="trd0">

      JS table新增一行的时候 如何在新增的这一行把样式也加进去 例如变成<tr class="trd0">5 JS: var tab = document.get ...

  5. 设置UILabel可变高度(根据文本内容自动适应高度)

    @property(nonatomic)UILabel *showLabel;   // 计算文本所占高度,计算出来之后设置label的高度 // 第一个参数:字体大小,字体大小/样式影响计算字体的高 ...

  6. jQuery中获取文档的高度、可视区域高度以及滚动条距页面顶部的高度

    在写页面的时候,经常会碰到这样的情况,就是要获取文档的高度.可视区域高度或者滚动条距页面顶部的高度等情况. 但我总是有些爱搞混淆了,这里还是简单做个笔记吧,这里只限于使用jQuery来获取. 1.获取 ...

  7. 父元素高度设置为min-height,子元素高度设置为100%,但实际上子元素高度你知道是多少吗?

    前言 给父元素一个min-height,子元素设置height:100%. 代码 <!DOCTYPE html> <html> <head> <title&g ...

  8. C#如何测量字符串的高度宽度和精确取得字符串的高度宽度

    C#如何测量字符串的高度宽度和精确取得字符串的高度宽度 因为MFC中CDC有GetTextExtent()可以获得字符串的高度宽度 像素单位,所以自然想到c#的GDI+的MeasureString,这 ...

  9. hive一行变多行及多行变一行

    hive一行变多行及多行变一行 场景 name alias zhaoqiansun abc def ghi 处理数据时需要将上表处理成为下面的形式: name alias zhaoqiansun ab ...

  10. js获取窗口滚动条高度、窗口可视范围高度、文档实际内容高度、滚动条离浏览器底部的高度

    1.获取窗口可视范围的高度 //获取窗口可视范围的高度 function getClientHeight(){ var clientHeight=0; if(document.body.clientH ...

随机推荐

  1. Android 发送短信总结

    SMS涉及的主要类SmsManager 实现SMS主要用到SmsManager类,该类继承自java.lang.Object类,下面我们介绍一下该类的主要成员. 公有方法: ArrayList< ...

  2. vue - vue-loader.conf

    'use strict' // 工具=> build/util.js const utils = require('./utils') // 配置=> build/config/index ...

  3. Nutch的发展历程(转)

    2002年8月由Doug Cutting发起,托管于Sourceforge,之后发布了0.4.0.5.0.6三个版本 2004年9月Oregon State University(俄勒冈州立大学)采用 ...

  4. 微信小程序之趣闻

    代码地址如下:http://www.demodashi.com/demo/13433.html 前言 小程序 的火热程度我就不多说了,我之前对这个就蛮有兴趣的,于是花了大概5天的时间,完成了 学习-入 ...

  5. iOS AVPlayer视频播放器

    代码地址如下:http://www.demodashi.com/demo/11168.html 一.运行效果 二.实现过程 ①.创建播放器avPlayer //创建播放器 url = [url str ...

  6. Chisel Tutorial(六)——port

    下面内容根据2015-7-10版的Chisel 2.2 Tutorial整理 port就是硬件单元对外的接口,须要指明方向(输入还是输出).一个port声明的样例例如以下: class Decoupl ...

  7. 【HTML+CSS】(2)CSS Sprite雪碧图

    1. 雪碧图的使用场景 (1). 静态图片.不随用户信息的变化而变化 (2). 小图片.图片容量比較小 (3). 载入量比較大 一些大图不建议拼成雪碧图,比如淘宝站点的导航图片都是使用的雪碧图. 2. ...

  8. 通过使用JanusGraph索引提高性能

    翻译整理:纪玉奇 Extending JanusGraph Server JanusGraph支持两种类型的索引:graph index和vertex-centric index.graph inde ...

  9. jquery ajax、get、post实例

    .post.$.get是一些简单的方法,如果要处理复杂的逻辑,还是需要用到jQuery.ajax() 一.$.ajax的一般格式 $.ajax({ type: 'POST', url: url , d ...

  10. Mac 全局变量 ~/.bash_profile 文件不存在的问题

    不存在就新建呗~ $ cd ~/ $ touch .bash_profile $ open -e .bash_profile 然后输入以下内容 # set color的部分是配置iterm2的字体颜色 ...