问题描述:

有时我们使用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死锁举例

    死锁: 在多线程竞争使用共享资源的情况下.就有可能出现死锁的情况.比方,当一个线程等待还有一个线程所持有的锁时.那个线程又可能在等待第一个线程所持有的锁.此时.这两个线程会陷入无休止的相互等待状态.这 ...

  2. Discuz常见小问题2-如何修改管理员密码,修改admin账户密码

    进入后台,点击用户,用户管理,搜索admin这个用户找到,然后点击详情   输入新密码即可(无需验证老的密码)          

  3. 《大话操作系统——做坚实的project实践派》(5)

    有人说我不是仅仅会玩X86吗?我如今铁板钉钉的申明:我事实上兼通:ARM.IA-32.IA-32e.AMD64. ​了解MIPS.但没做过MIPS由于确实没有对应的MIPS硬件平台. ​

  4. Java的内存机制(转)

    0.参考资料: http://www.j2megame.org/index.php/content/view/2246/125.html 1.Java的内存机制 Java 把内存划分成两种:一种是栈内 ...

  5. LNMP环境搭建——MySQL篇

    The world's most popular open source database 1.Install MySQL root@kallen:~# apt-get install mysql-s ...

  6. Mysql中的排序规则utf8_unicode_ci、utf8_general_ci的区别

    utf8_unicode_ci和utf8_general_ci对中.英文来说没有实质的差别.utf8_general_ci 校对速度快,但准确度稍差.utf8_unicode_ci 准确度高,但校对速 ...

  7. .net core 控制台程序使用依赖注入(Autofac)

    1.Autofac IOC 容器 ,便于在其他类获取注入的对象 using System; using System.Collections.Generic; using System.Linq; u ...

  8. 自定义asp.net mvc Filter 过滤器

    1新建一个mvc项目:如图 2.主要创建下面一些类文件 1.utility目录 放置自定义的过滤器 using System; using System.Collections.Generic; us ...

  9. AMQP && MQTT comparision

    1. AMQP (Advanced Message Queuing Protocol) 2. MQTT (Message Queuing Telemetry Transport) Introducti ...

  10. Linux间的进程通信;以及子进程的创建

    "-----第六天-----------------------------------------------------------------------------" .版 ...