Parth Mehrotra
Parth Mehrotra

Reputation: 3040

ScrollView inside a ViewPager

So I have a ScrollView as the first view of a ViewPager. The following code renders correctly, but it simply does not scroll.

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]);

    }
}

I know I must be overlooking something really silly but I can't find the solution anywhere.

Upvotes: 1

Views: 1807

Answers (1)

Cat
Cat

Reputation: 67522

The issue appears to be with your Patterns. Since they are simply wrappers for a Canvas, they do not have any content to give them a height. Giving them fixed heights will ensure that all their content is shown, and will allow the ScrollView to account for this, and allow you to scroll properly.

Upvotes: 1

Related Questions