Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6603975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:05:22+00:00 2026-05-25T19:05:22+00:00

Like this previous person , I have unwanted overlap between GridView items: Notice the

  • 0

Like this previous person, I have unwanted overlap between GridView items:

GridView items overlapping

Notice the text, in every column except the rightmost one.

Where I differ from that previous question is that I don’t want a constant row height. I want the row height to vary to accommodate the tallest content in each row, for efficient use of screen space.

Looking at the source for GridView (not the authoritative copy, but kernel.org is still down), we can see in fillDown() and makeRow() that the last View seen is the “reference view”: the row’s height is set from the height of that View, not from the tallest one. This explains why the rightmost column is ok. Unfortunately, GridView is not well set-up for me to fix this by inheritance. All the relevant fields and methods are private.

So, before I take the well-worn bloaty path of “clone and own”, is there a trick I’m missing here? I could use a TableLayout, but that would require me to implement numColumns="auto_fit" myself (since I want e.g. just one long column on a phone screen), and it also wouldn’t be an AdapterView, which this feels like it ought to be.

Edit: in fact, clone and own is not practical here. GridView depends on inaccessible parts of its parent and sibling classes, and would result in importing at least 6000 lines of code (AbsListView, AdapterView, etc.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:05:22+00:00Added an answer on May 25, 2026 at 7:05 pm

    I used a static array to drive max heights for the row. This is not perfect since the earlier columns will not be resized until the cell is redisplayed. Here is the code for the inflated reusable content view.

    Edit: I got this work correctly but I had pre-measure all cells before rendering. I did this by subclassing GridView and adding a measuring hook in the onLayout method.

    /**
     * Custom view group that shares a common max height
     * @author Chase Colburn
     */
    public class GridViewItemLayout extends LinearLayout {
    
        // Array of max cell heights for each row
        private static int[] mMaxRowHeight;
    
        // The number of columns in the grid view
        private static int mNumColumns;
    
        // The position of the view cell
        private int mPosition;
    
        // Public constructor
        public GridViewItemLayout(Context context) {
            super(context);
        }
    
        // Public constructor
        public GridViewItemLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        /**
         * Set the position of the view cell
         * @param position
         */
        public void setPosition(int position) {
            mPosition = position;
        }
    
        /**
         * Set the number of columns and item count in order to accurately store the
         * max height for each row. This must be called whenever there is a change to the layout
         * or content data.
         * 
         * @param numColumns
         * @param itemCount
         */
        public static void initItemLayout(int numColumns, int itemCount) {
            mNumColumns = numColumns;
            mMaxRowHeight = new int[itemCount];
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            // Do not calculate max height if column count is only one
            if(mNumColumns <= 1 || mMaxRowHeight == null) {
                return;
            }
    
            // Get the current view cell index for the grid row
            int rowIndex = mPosition / mNumColumns;
            // Get the measured height for this layout
            int measuredHeight = getMeasuredHeight();
            // If the current height is larger than previous measurements, update the array
            if(measuredHeight > mMaxRowHeight[rowIndex]) {
                mMaxRowHeight[rowIndex] = measuredHeight;
            }
            // Update the dimensions of the layout to reflect the max height
            setMeasuredDimension(getMeasuredWidth(), mMaxRowHeight[rowIndex]);
        }
    }
    

    Here is the measuring function in my BaseAdapter subclass. Note that I have a method updateItemDisplay that sets all appropriate text and images on the view cell.

        /**
         * Run a pass through each item and force a measure to determine the max height for each row
         */
        public void measureItems(int columnWidth) {
            // Obtain system inflater
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // Inflate temp layout object for measuring
            GridViewItemLayout itemView = (GridViewItemLayout)inflater.inflate(R.layout.list_confirm_item, null);
    
            // Create measuring specs
            int widthMeasureSpec = MeasureSpec.makeMeasureSpec(columnWidth, MeasureSpec.EXACTLY);
            int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    
            // Loop through each data object
            for(int index = 0; index < mItems.size(); index++) {
                String[] item = mItems.get(index);
    
                // Set position and data
                itemView.setPosition(index);
                itemView.updateItemDisplay(item, mLanguage);
    
                // Force measuring
                itemView.requestLayout();
                itemView.measure(widthMeasureSpec, heightMeasureSpec);
            }
        }
    

    And finally, here is the GridView subclass set up to measure view cells during layout:

    /**
     * Custom subclass of grid view to measure all view cells
     * in order to determine the max height of the row
     * 
     * @author Chase Colburn
     */
    public class AutoMeasureGridView extends GridView {
    
        public AutoMeasureGridView(Context context) {
            super(context);
        }
    
        public AutoMeasureGridView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public AutoMeasureGridView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            if(changed) {
                CustomAdapter adapter = (CustomAdapter)getAdapter();
    
                int numColumns = getContext().getResources().getInteger(R.integer.list_num_columns);
                GridViewItemLayout.initItemLayout(numColumns, adapter.getCount());
    
                if(numColumns > 1) {
                    int columnWidth = getMeasuredWidth() / numColumns;
                    adapter.measureItems(columnWidth);
                }
            }
            super.onLayout(changed, l, t, r, b);
        }
    }
    

    The reason I have the number of columns as a resource is so that I can have a different number based on orientation, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a previous life, I might have done something like this: <a href=# onclick=f(311);return
Much like this previous question , I wish to store a MySQL column in
Like this question , except T-SQL instead of php. 206275947 = 2062759.47 etc. The
I need a query that can sum like this (previous row with current row)
I have a struct like so typedef struct person { int id; char name[20];
This is an extension to a previous question which was answered. But have a
This is a slight update to my previous question I have a Python list
I have a repository pattern setup using NHibernate. The base class looks like this:
I have some lines like this from an ldiff file, dn: cn=dkalland_directs_ww,cn=org_groups,cn=beehive_groups,cn=groups,dc=oracle ,dc=com businesscategory:
I have an iframe like this : <iframe width=985px frameborder=0 marginheight=0 marginwidth=0 height=2100px src=http://www.myblog.com/search?q=<?php

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.