在Android中的Horizo​​ntal ScrollView中显示更多内容

时间:2013-12-23 12:53:29

标签: android android-view android-scrollview

我不确定什么是最好的标题,但有了图片,我很清楚我想要的是什么。

我已经实现了Horizo​​ntal Scrollview,我已经定制了它,因为只显示了四个项目,如果用户想看第五个项目,那么他必须滚动它。 我成功地完成了它。

但是在android 2.3.3中,当有更多项目时,它显示白色视图,而在android 4.0中它没有显示。见下图:

enter image description here

在android 2.3中查看,我正在显示白色视图,它清楚地告诉我,有更多按钮,但同样的结果我没有进入android 4.0或更高版本。
任何人都可以帮我如何显示它。

1 个答案:

答案 0 :(得分:3)

您可以通过扩展HorizontalScrollView小部件并绘制两个正确放置的图像/绘图来轻松复制该行为:

public class CustomHorizontalScrollView extends HorizontalScrollView {

    private static final int SHADOW_WIDTH = 35;
    private GradientDrawable mDrawableLeft;
    private GradientDrawable mDrawableRight;
    private Rect mBounds = new Rect();

    public CustomHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mDrawableLeft = new GradientDrawable(Orientation.LEFT_RIGHT,
                new int[] { Color.GRAY, Color.TRANSPARENT });
        mDrawableRight = new GradientDrawable(Orientation.RIGHT_LEFT,
                new int[] { Color.GRAY, Color.TRANSPARENT });
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        // the scroll value
        final int offset = this.getScrollX();
        mBounds.setEmpty();
        mBounds.bottom = getMeasuredHeight();
        // check made to remove the shadow if we are at the left edge of the
        // screen so we don't interfere with the edge effect
        if (offset != 0) {
            // left drawable
            mBounds.left = offset;
            mBounds.right = offset + SHADOW_WIDTH;
            mDrawableLeft.setBounds(mBounds);
            mDrawableLeft.draw(canvas);
        }
        // check made to remove the shadow if we are at the right edge of the
        // screen so we don't interfere with the edge effect
        if ((offset + getMeasuredWidth()) < computeHorizontalScrollRange()) {
            // right drawable
            mBounds.left = offset + getMeasuredWidth() - SHADOW_WIDTH;
            mBounds.right = offset + getMeasuredWidth();
            mDrawableRight.setBounds(mBounds);
            mDrawableRight.draw(canvas);
        }
    }

}