ViewPager中的ScrollView

时间:2012-08-31 18:14:12

标签: android scrollview android-viewpager

所以我有ScrollView作为ViewPager的第一个视图。以下代码正确呈现,但它不会滚动。

ShadeDisplay.java

public class ShadeDisplay extends SherlockFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle ParthMehrotra) {
        if (container == null) {
            return null;
        }

        ScrollView sv = new ScrollView(getSherlockActivity());
        sv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.FILL_PARENT));
        sv.setFillViewport(true);
        sv.setBackgroundColor(Color.BLACK);

        LinearLayout ll = new LinearLayout(getSherlockActivity());
        ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT));
        ll.setOrientation(LinearLayout.VERTICAL);

        ImageView iv = new ImageView(getSherlockActivity());
        iv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT));
        iv.setImageResource(R.drawable.banner);
        ll.addView(iv);

        Pattern patern1 = new Pattern(getSherlockActivity());
        patern1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        Pattern patern2 = new Pattern(getSherlockActivity());
        patern2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        ll.addView(patern1);
        ll.addView(patern2);

        sv.addView(ll);
        return sv;
    }

}

Pattern.java

public class Pattern extends View {

    Paint[] shade = new Paint[5];

    public Pattern(Context context) {
        super(context);

        for (int i = 0; i < shade.length; i++) {
            shade[i] = new Paint();
        }

        shade[0].setColor(Color.RED);
        shade[1].setColor(Color.BLUE);
        shade[2].setColor(Color.GREEN);
        shade[3].setColor(Color.YELLOW);
        shade[4].setColor(Color.CYAN);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawRect(0, 0, getWidth(), getWidth() / 3, shade[0]);
        canvas.drawRect(((getWidth() / 3) * 2), 0, getWidth(), getWidth(),
                shade[1]); 
        canvas.drawRect(0, ((getWidth() / 3) * 2), getWidth(), getWidth(),
                shade[2]);
        canvas.drawRect(0, (getWidth() / 3), getWidth()/3, getWidth(), 
                shade[3]);
        canvas.drawRect(getWidth() / 3, getWidth() / 3, ((getWidth() / 3) * 2),
                ((getWidth() / 3) * 2), shade[4]);

    }
}

我知道我必须忽略一些非常愚蠢的东西,但我无法在任何地方找到解决方案。

1 个答案:

答案 0 :(得分:1)

问题似乎与您的Pattern有关。由于它们只是Canvas的包装器,因此它们没有任何内容可以为它们提供高度。给予它们固定的高度将确保显示所有内容,并允许ScrollView对此进行说明,并允许您正确滚动。