问题描述:

有时我们使用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. Java I/O操作汇总

    作者:卿笃军 原文地址:http://blog.csdn.net/qingdujun/article/details/41154807 本文简绍解说了FileWriter.FileReader.Buf ...

  2. 投票ajax请求代码(点赞代码)

    function vote(url, arr) { jq.ajax({ cache: false, async: false, url: url, type: 'post', data: {info_ ...

  3. Android Exception 7(attempt to re-open an already-closed object )

    07-23 18:16:17.641: W/SQLiteConnectionPool(28390): A SQLiteConnection object for database '/storage/ ...

  4. 【java】对数据库操作的那些事(包含数据库中的预处理)

    一.连接问题 前面刚介绍了怎么连接数据库,也写了对应的模板.可是它的可维护性很差.那么怎么解决问题呢? 首先写一个配置文件jdbc.properties <span style="fo ...

  5. ORA-01589: 要打开数据库则必须使用 RESETLOGS 或 NORESETLOGS 选项

    产生这个的原因可能是由于数据库突然停止,没有来得及将缓存区中的LOG归档,导致下次开启时不能匹配日志文件. 数据库中的三个日志文件挨个试,第二个就匹配上了

  6. lsb_release: command not found 解决

    问题:lsb_release 是查看系统版本信息的工具 [root@localhost ~]# lsb_release -a-bash: lsb_release: command not found ...

  7. Coreseek-带中文分词的Sphinx

    Sphinx并不支持中文分词, 也就不支持中文搜索, Coreseek = Sphinx + MMSEG(中文分词算法) 1.下载 1).到官网下载 2).解压后有三个文件夹 csft-3.2.14: ...

  8. 自己动手,制作真正的.net Framework 3.5 SP1离线安装包(转)

    .NET Framework 3.5 SP1发布了,兴冲冲…下载,当然是完全离线安装包.net Framework 3.5 Service pack 1(Full Package) 231MB,网络慢 ...

  9. 用Web API Client 调用 Web API

    安装Web API客户端库 右键单击项目,选择管理 NuGet程序包,选择联机选项,选择全部,在搜索框中输入“Microsoft.AspNet.WebApi.Client”, 搜索结果就是要安装的类库 ...

  10. Git使用总结 Asp.net生命周期与Http协议 托管代码与非托管代码的区别 通过IEnumerable接口遍历数据 依赖注入与控制反转 C#多线程——优先级 AutoFac容器初步 C#特性详解 C#特性详解 WPF 可触摸移动的ScrollViewer控件 .NET(C#)能开发出什么样的APP?盘点那些通过Smobiler开发的移动应用

    一,原理 首先,我们要明白Git是什么,它是一个管理工具或软件,用来管理什么的呢?当然是在软件开发过程中管理软件或者文件的不同版本的工具,一些作家也可以用这个管理自己创作的文本文件,由Linus开发的 ...