Custom a GridView like win 8 style

1.5k views Asked by At

I need to custom a gridview that can be showed like following pic enter image description here

As you see, every patch's width and height is same.I have custom a gridview, but encounter a problem.when the screen being scrolled, the onMeasure() method is called many times.Even you stop to scroll the screen, it also being called.

public class FixRowGridView extends GridView {

private static final String TAG = "FixRowGridView";

private int mChildHeight;

public FixRowGridView(Context context, AttributeSet attrs, int defStyle) {

    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub

}

public FixRowGridView(Context context, AttributeSet attrs) {

    super(context, attrs);
    // TODO Auto-generated constructor stub

}

public FixRowGridView(Context context) {

    super(context);
    // TODO Auto-generated constructor stub

}

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

    // TODO Auto-generated method stub
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    mChildHeight = (height - 3 * DimensionAdjustUtil.dipToPix(getContext(), 10)) / 4;
    int width = (mChildHeight << 1) + DimensionAdjustUtil.dipToPix(getContext(), 10);
    setMeasuredDimension(width, height);

    DumpUtil.dLog(TAG, "The height is " + height);
    /*int width = (mChildHeight << 1) + DimensionAdjustUtil.dipToPix(getContext(), 10);
    setMeasuredDimension(width, height);*/
    /*setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), 
            MeasureSpec.getSize(heightMeasureSpec));*/
}

@Override
protected void layoutChildren() {

    super.layoutChildren();

    int count = getChildCount();

    for(int i = 0; i < count; i++) {
        View view = getChildAt(i);
        LayoutParams params = (LayoutParams) view.getLayoutParams();
        params.height = mChildHeight;
        params.width = mChildHeight;
        view.setLayoutParams(params);
    }

}

}

Apparently, the code is expensive. I'm new to ViewGroup class. If you can help me improve the efficiency or provide a third library, please let me know. Any help will be appreciated!

1

There are 1 answers

0
Sysphus On

The question has been resolved, the method is following: 1.Calculating the right height and width according to your phone's size. 2.Set a fixed value that got from step 1 to the item.

Hope it will help you.