使用View Pager用于没有Gallery的自定义Gallery视图

时间:2013-12-30 04:40:45

标签: android android-layout view android-viewpager android-gallery

我正在创建一个应用程序,我在其中使用自定义图库视图(不使用Gallery,因为它已弃用)。我现在在覆盖onMeasure()后仍然使用View Pager for Horizo​​ntal scroll,我的代码片段在下面

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

    boolean wrapHeight = MeasureSpec.getMode(heightMeasureSpec) 
            == MeasureSpec.AT_MOST;

    if(wrapHeight) {

        int width = getMeasuredWidth(), height = getMeasuredHeight();

        // Use the previously measured width but simplify the calculations
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);

        /* If the pager actually has any children, take the first child's 
         * height and call that our own */ 
        if(getChildCount() > 0) {
            View firstChild = getChildAt(0);

            /* The child was previously measured with exactly the full height.
             * Allow it to wrap this time around. */
            firstChild.measure(widthMeasureSpec, 
                    MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST));

            height = firstChild.getMeasuredHeight();
        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

0 个答案:

没有答案