Horizo​​ntalScrollView不滚动

时间:2015-04-14 21:29:25

标签: android scrollview horizontalscrollview

我有一个HorizontalScrollView和一个GridLayout里面有3张图片。它永远不会像我想象的那样滚动,无论我做什么 - 用手势,触摸 - 什么都不会使水平滚动。

我的XML:

<com.app4u.borala.atividades.layout.GridEventoPesqGeo
    android:id="@+id/horizontalScroll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:background="@color/azulActionBarTransparente"
    android:fillViewport="true"
    android:scrollbars="horizontal" >

    <GridLayout
        android:id="@+id/gridEventos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rowCount="1" >

        <com.app4u.borala.atividades.layout.ImageViewEventoPesqGeo
            android:id="@+id/imageEvento1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@drawable/background_evento_temp" />

        <com.app4u.borala.atividades.layout.ImageViewEventoPesqGeo
            android:id="@+id/imageEvento2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@drawable/background_evento_temp" />

        <com.app4u.borala.atividades.layout.ImageViewEventoPesqGeo
            android:id="@+id/imageEvento3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@drawable/background_evento_temp" />
    </GridLayout>
</com.app4u.borala.atividades.layout.GridEventoPesqGeo>

我的班级GridEventoPesqGeo:

public class GridEventoPesqGeo extends HorizontalScrollView {

public GridEventoPesqGeo(Context context) {
    super(context);
}

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

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

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

    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);

    this.setMeasuredDimension(parentWidth, (int) (parentHeight * 0.225));
}
}

我的类ImageViewEventoPesqGeo:

public class ImageViewEventoPesqGeo extends ImageView{

public ImageViewEventoPesqGeo(Context context) {
    super(context);
}

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

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

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

    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    //int parentHeight = MeasureSpec.getSize(heightMeasureSpec);

    MarginLayoutParams lpimgFooter = (MarginLayoutParams) getLayoutParams();
    lpimgFooter.bottomMargin = (int) (parentWidth * 0.015);
    lpimgFooter.leftMargin = (int) (parentWidth * 0.015);
    lpimgFooter.rightMargin = (int) (parentWidth * 0.015);
    lpimgFooter.topMargin = (int) (parentWidth * 0.015);
    setLayoutParams(lpimgFooter);

    this.setMeasuredDimension((int) (parentWidth * 0.33), (int) (parentWidth * 0.33));
}
}

我的Java课程只会使widthheight成比例,具体取决于手机的屏幕尺寸。

1 个答案:

答案 0 :(得分:0)

以下是HorizontalScrollView onMeasure()的来源:

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

    if (!mFillViewport) {
        return;
    }

    final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    if (widthMode == MeasureSpec.UNSPECIFIED) {
        return;
    }

    if (getChildCount() > 0) {
        final View child = getChildAt(0);
        int width = getMeasuredWidth();
        if (child.getMeasuredWidth() < width) {
            final FrameLayout.LayoutParams lp = (LayoutParams) child.getLayoutParams();

            int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, mPaddingTop
                    + mPaddingBottom, lp.height);
            width -= mPaddingLeft;
            width -= mPaddingRight;
            int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);

            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
        }
    }
}

请注意,如果mFillViewPort为true,它将在没有到达您的代码的情况下返回。由于您在xml中将其设置为true,因此难怪您的代码未执行。