سؤال

I would like to display all items (SquareTextView) in GridView but on some resolution not all item are visible and user need to scroll. I would like to show all content for user without scrolling.

SquareTextView contain methods (Adding this method to CGridView does not help):

  @Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec)
{
    final int width = getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
    setMeasuredDimension(width, width);
}

@Override
protected void onSizeChanged(final int w, final int h, final int oldw, final int oldh)
{
    super.onSizeChanged(w, w, oldw, oldh);
}

//items are matching their parent How should I calculate Grid width/height to display all content? I look at: Gridview height gets cut but answer does not help.

هل كانت مفيدة؟

المحلول

I fixed the problem adding to custom gridView:

  @Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    final int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
    final int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
    if (width > height) {
        setMeasuredDimension(height, height);
    } else {
        setMeasuredDimension(width, width);
    }


    Log.v(TAG, "sizes are h: " + height + " w: " + width);
}


@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    /**
     * Changed Size of TextView from "width" x "height" to "width" x "width"
     * to look like square*/
    super.onSizeChanged(w, w, oldw, oldh);

    /**
     * Hack -
     * if height is bigger then width
     * set height size to width
     * if width is bigger than height then
     * set width size to height
     */
    Log.v(TAG, "sizes2 are h: " + h + " w: " + w);
    if (getLayoutParams() != null && w < h) {
        getLayoutParams().height = w;
        setLayoutParams(getLayoutParams());
    }
    else if(getLayoutParams() != null && w > h){
        getLayoutParams().width = h;
        setLayoutParams(getLayoutParams());
    }
} 

With this my gridView looks like square and all item are visible on devices. Should help if someone had the same issue.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top