Android: make ImageViews standing in one row of GridView have equal height

161 Views Asked by At

I have a GridLayout with 1-3 columns, depends on orientation & screen size. Each GridLayout item is a vertical LinearLayout with ImageView and others (LinearLayout with TextViews etc). Images have different sizes. I've written a simple algorithm to crop images. Sometimes it works fine (e. g. when slowly scrolling), but sometimes I cannot see the result at all; items can disappear during fast scroll. I know SquareImageView, but my images are horizontal (landscape), not square. I want to set image height for all items in one row.

My GridView looks like this:

 ________________      ________________
|                |    |                |
|                |    |                |
|   ImageView    |    |   ImageView    |
|                | :( |________________|
|________________|   ^| LL, text, etc  |
| LL, text, etc  |^   |________________|
|________________|

After setAdapter:

//gvStocks is my GridView
gvStocks.setOnScrollListener(new AbsListView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {}

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        //MainHelper is an abstract class containing methods & code snippets
        MainHelper.adjustImgHeights(gvStocks, firstVisibleItem, visibleItemCount);
    }
});
gvStocks.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        MainHelper.adjustImgHeights(gvStocks);
        if (Build.VERSION.SDK_INT >= 16)
            gvStocks.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
});

MainHelper.adjustImgHeights:

public static void adjustImgHeights(GridView gvStocks) {
    int first = gvStocks.getFirstVisiblePosition();
    adjustImgHeights(gvStocks, first, gvStocks.getLastVisiblePosition() - first + 1);
}

public static void adjustImgHeights(GridView gvStocks, int firstVisibleItem, int visibleItemCount) {
    int cols = gvStocks.getNumColumns();
    for (int curPos = firstVisibleItem; curPos < firstVisibleItem + visibleItemCount; curPos++) {
        int first = (curPos + 1) - (curPos) % cols - 1;
        int minHeight = Integer.MAX_VALUE;

        //get min height
        for (byte b = 0; b < cols; b++) {
            ViewGroup item = (ViewGroup) gvStocks.getChildAt(first + b);
            if (item != null) {
                ImageView iv = (ImageView) item.findViewById(R.id.ivStock);
                int height = iv.getHeight();
                if (height != 0) {
                    if (height < minHeight)
                        minHeight = height;
                }
            }
        }
        if (minHeight != Integer.MAX_VALUE) {
            //set min height
            for (byte b = 0; b < cols; b++) {
                ViewGroup item = (ViewGroup) gvStocks.getChildAt(first + b);
                if (item != null) {
                    ImageView iv = (ImageView) item.findViewById(R.id.ivStock);
                    iv.getLayoutParams().height = minHeight;
                }
            }
        }
    }
}

What's wrong with my code?

0

There are 0 best solutions below