草庐IT

Android GridView 行高是最后一列的行高

coder 2023-12-22 原文

所以我有一个可展开 View 的 gridview(用户点击它们,它们会动画展开)。问题是,当我单击第二列时,我的 gridview 只会增加行高。在第一张图片中,您可以看到我按下了列表中的第一项,它展开了,但是 gridview 没有更新它的行高。在第二张图片中,我单击了第二列中的第二个项目,它和 gridview 按预期展开。似乎 gridview 仅基于第二列进行测量。我怎样才能让它与第一列进行比较并选择较大的一列?谢谢。

编辑: 这甚至发生在普通布局中。行宽始终是第二列的高度,即使该行中的第一列更高。

最佳答案

我遇到了同样的问题,为了解决这个问题,我不得不扩展 GridView 并使用一些肮脏的 hacks...

public class AutoGridView extends GridView {
    public AutoGridView(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
    }

    public AutoGridView(Context context, AttributeSet attrs){
        super(context, attrs);
    }

    public AutoGridView(Context context){
        super(context);
    }

    @Override
    protected void layoutChildren(){
    /*This method is called during onLayout. I let the superclass make the hard
    part, then I change the top and bottom of visible items according to their
    actual height and that of their siblings*/

        final int childrenCount = getChildCount();
        if(childrenCount <= 0){ //nothing to do, just call the super implementation
            super.layoutChildren();
            return;
        }

        /*GridView uses the bottom of the last child to decide if and how to scroll.
        UGLY HACK: ensure the last child bottom is equal to the lowest one.
        Since super implementation might invalidate layout I must be sure the old
        value is restored after the call*/
        View v = getChildAt(childrenCount - 1);
        int oldBottom = v.getBottom();
        super.layoutChildren();
        v.setBottom(oldBottom);

        for(int i = 0; i < getNumColumns(); ++i){
            int top = getListPaddingTop();
            for(int j = i; j < childrenCount; j += getNumColumns()){
                v = getChildAt(j);
                /*after setTop v.getHeight() returns a different value,
                that's why I'm saving it...*/
                int viewHeight = v.getHeight();
                v.setTop(top);
                top += viewHeight;
                v.setBottom(top);
                top += getVerticalSpacing();
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        final int childrenCount = getChildCount();
        if(getAdapter() != null && childrenCount > 0){
            measureChildren(widthMeasureSpec, heightMeasureSpec);
            int width = getSize(widthMeasureSpec);
            int heightSize = getSize(heightMeasureSpec);
            int heightMode = getMode(heightMeasureSpec);

            int maxHeight = heightSize;
            for(int i = 0; i < getNumColumns(); ++i){
                int columnHeight = 0;
                for(int j = i; j < childrenCount; j += getNumColumns())
                    columnHeight += getChildAt(j).getMeasuredHeight() + getVerticalSpacing();
                maxHeight = Math.max(maxHeight, columnHeight);
            }

            getChildAt(childrenCount-1).setBottom(maxHeight); // for the scrolling hack

            int height = heightSize;
            if(heightMode == UNSPECIFIED)
                height = maxHeight;
            else if(heightMode == AT_MOST)
                height = Math.min(maxHeight, heightSize);

            setMeasuredDimension(width, height);
        }
    }
}

我知道,这个答案对你来说可能为时已晚,但我希望它能帮助像我一样遇到问题的人 ^^

关于Android GridView 行高是最后一列的行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18155053/

有关Android GridView 行高是最后一列的行高的更多相关文章

  1. ruby-on-rails - 使用 ruby​​ 将多个实例变量转换为散列的更好方法? - 2

    我收到格式为的回复#我需要将其转换为哈希值(针对活跃商家)。目前我正在遍历变量并执行此操作:response.instance_variables.eachdo|r|my_hash.merge!(r.to_s.delete("@").intern=>response.instance_eval(r.to_s.delete("@")))end这有效,它将生成{:first="charlie",:last=>"kelly"},但它似乎有点hacky和不稳定。有更好的方法吗?编辑:我刚刚意识到我可以使用instance_variable_get作为该等式的第二部分,但这仍然是主要问题。

  2. ruby - Hanami link_to 助手只呈现最后一个元素 - 2

    我是HanamiWorld的新人。我已经写了这段代码:moduleWeb::Views::HomeclassIndexincludeWeb::ViewincludeHanami::Helpers::HtmlHelperdeftitlehtml.headerdoh1'Testsearchengine',id:'title'hrdiv(id:'test')dolink_to('Home',"/",class:'mnu_orizontal')link_to('About',"/",class:'mnu_orizontal')endendendendend我在模板上调用了title方法。htm

  3. Ruby:如何使用带有散列的 'send' 方法调用方法? - 2

    假设我有一个类A,里面有一些方法。假设stringmethodName是这些方法之一,我已经知道我想给它什么参数。它们在散列中{'param1'=>value1,'param2'=>value2}所以我有:params={'param1'=>value1,'param2'=>value2}a=A.new()a.send(methodName,value1,value2)#callmethodnamewithbothparams我希望能够通过传递我的哈希以某种方式调用该方法。这可能吗? 最佳答案 确保methodName是一个符号,而

  4. ruby - 如果它是标点符号,我怎么能从字符串中删除最后一个字符,在 ruby​​ 中? - 2

    啊,正则表达式有点困惑。我正在尝试删除字符串末尾所有可能的标点符号:ifstr[str.length-1]=='?'||str[str.length-1]=='.'||str[str.length-1]=='!'orstr[str.length-1]==','||str[str.length-1]==';'str.chomp!end我相信有更好的方法来做到这一点。有什么指点吗? 最佳答案 str.sub!(/[?.!,;]?$/,'')[?.!,;]-字符类。匹配这5个字符中的任何一个(注意,。在字符类中并不特殊)?-前一个字符或组

  5. Ruby - 删除文件中的最后一个字符? - 2

    看起来一定很简单,但我就是想不通。如何使用RubyIO删除文件的最后一个字符?我查看了deletingthelastlineofafile的答案使用Ruby但没有完全理解它,必须有更简单的方法。有什么帮助吗? 最佳答案 有File.truncate:truncate(file_name,integer)→0Truncatesthefilefile_nametobeatmostintegerbyteslong.Notavailableonallplatforms.所以你可以这样说:File.truncate(file_name,Fil

  6. ruby-on-rails - Ruby on Rails 的最后 20% - 2

    我是(相当)一位经验丰富的程序员,但对Ruby和RubyonRails完全陌生。RoR看起来很适合快速工作,特别是用于CRUD操作的自动屏幕生成。它确实能让您快速提高工作效率。问题是最后20%的工作,那时我必须完成我的申请。RoR公约不会妨碍我吗?因为不是每个数据库表都必须对所有用户可用,也不是所有用户都可以编辑所有列和/或所有行,而且View必须适应我网站的外观等。我知道RoR已成功用于现场,但在第一阶段烧毁后,如何在RoR中获得足够的速度以逃避重力。 最佳答案 我认为脚手架无法让您达到80%。脚手架很好,因为它向您展示了Rail

  7. arrays - 在一行中选择数组的第一个和最后一个元素 - 2

    我的任务是从数组中选择最高和最低的数字。我想我很清楚我想做什么,但只是努力以正确的格式访问信息以满足通过标准。defhigh_and_low(numbers)array=numbers.split("").map!{|x|x.to_i}array.sort!{|a,b|ba}putsarray[0,-1]end数字可能看起来像"80917234100",要通过,我需要输出"9234"。我正在尝试putsarray.first.last,但一直无法弄明白。 最佳答案 有Array#minmax完全满足您需要的方法:array=[80,

  8. ruby-on-rails - 是否可以让 ActiveRecord 为使用 :joins option? 加载的行创建对象 - 2

    我需要做这样的事情classUser'User',:foreign_key=>'abuser_id'belongs_to:gameendclassGame['JOINabuse_reportsONusers.id=abuse_reports.abuser_id','JOINgamesONgames.id=abuse_reports.game_id'],:group=>'users.id',:select=>'users.*,count(distinctgames.id)ASgame_count,count(abuse_reports.id)asabuse_report_count',:

  9. 要散列的 Ruby 数组 - 2

    我有以下数组:myarray=[['usa','primary','john'],['france','primary','lira'],['usa','secondary','steve'],['germany','primary','jeff'],['france','secondary','ben']]我想将它转换成一个散列数组,如下所示:[{:country=>'usa',:primary=>'john',:secondary=>'steve'},{:country=>'france',:primary=>'lira',:secondary=>'ben'},{:country=

  10. ruby-on-rails - 向现有表添加非空列的问题 - 2

    我在向表中添加不可为空的列时遇到问题。我看了很多关于这个的帖子,它应该是正确的。迁移代码:defchangeadd_column:individual_trainings,:start_on,:timeadd_column:individual_trainings,:end_on,:timechange_column_null:individual_trainings,:start_on,falsechange_column_null:individual_trainings,:end_on,falseend错误:PG::NotNullViolation:ERROR:column"st

随机推荐