• GridView解决同一行item的高度不一样,如何同一行统一高度问题?


    问题描述:

    有时我们使用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  看介绍可以实现瀑布流哦  具体自行了解下

  • 相关阅读:
    【个人博客设计】
    复杂多边形光栅化算法
    xmake 描述语法和作用域详解
    协程分析之context上下文切换
    tbox协程使用之切换与等待
    记boost协程切换bug发现和分析
    跨平台自动构建工具v1.0.2 发布
    xmake从入门到精通9:交叉编译详解
    xmake从入门到精通9:交叉编译详解
    跨平台c开发库tbox:内存库使用详解
  • 原文地址:https://www.cnblogs.com/woaixingxing/p/9396484.html
Copyright © 2020-2023  润新知