aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/ui/WrappingGridView.java
blob: 2c858874d1864382b0450608616fbc44e12f11af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package cgeo.geocaching.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;

/**
 * GridView that will adjust its height to really use wrap_content. The standard GridView only shows one line of items.
 *
 * @see <a href="https://gist.github.com/runemart/9781609">https://gist.github.com/runemart/9781609</a>
 *
 */
public class WrappingGridView extends GridView {

    public WrappingGridView(final Context context) {
        super(context);
    }

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

    public WrappingGridView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
        int heightSpec = heightMeasureSpec;
        if (getLayoutParams().height == android.view.ViewGroup.LayoutParams.WRAP_CONTENT) {
            // The two leftmost bits in the height measure spec have
            // a special meaning, hence we can't use them to describe height.
            heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        }
        super.onMeasure(widthMeasureSpec, heightSpec);
    }

}