7

I use the following user interface:

  • A parent relative layout parentLayout with the dimensions 800x600 (width x height)

  • A second relative layout childLayout, which is a child of the parent layout. It has the dimensions 800x1000, i.e. it is larger than the parent layout.

    parentLayout.addView(childLayout);
    

My goal: Scrolling childLayout by using childLayout.scrollTo(x,y).

When I use childLayout.scrollTo(x,y), Android scrolls childLayout but doesn't refresh (redraw) it. The effect is, that childLayout is cut to the same dimensions as parentLayout.

Unfortunately, the following solutions don't solve the problem:

childLayout.scrollTo(x,y);
childLayout.invalidate();
childLayout.requestLayout();

Any ideas how to solve this problem?

Adrian Heine
  • 4,051
  • 2
  • 30
  • 43
Anne Droid
  • 3,151
  • 4
  • 16
  • 15
  • Am I the only one not understanding the problem "The effect is, that childLayout is cut to the same dimensions as parentLayout"? You mean the visible part or the whole scrollview? Could you add some kind of sketch? – User Aug 21 '12 at 17:50
  • Your understanding is correct. Android scrolls only the visible part of childLayout. The visible part of childLayout is defined by it's parent view parentLayout. I.e. in my example the visible part of childLayout has the dimensions 800x600. If I use childLayout.scrollTo(), a refresh should be necessary in order to render childLayout and to make the invisible part visible. My example has nothing to do with Android's ScrollView. – Anne Droid Aug 21 '12 at 20:40
  • It could be that the parent clips the child to the visible area in the moment you add it. Maybe it helps to add clipChildren=false to the parent? Or calling invalidate (etc.) also on the parent, after you scrolled? Or try using a ScrollView as parent and scroll this scrollView. – User Aug 22 '12 at 08:21
  • Thank you lxx for your idea, but the method setClipChildren(false) does not solve the problem. But I want to share a workaround with you: If the height of childLayout is set to WRAP_CONTENT, then Android clips childLayout and the invisible part is not scrolled. BUT when the height is set to an absolute value e.g. height=1000 then childLayout is not cliped! So, at the moment my solution is, to use an absolute height value for childLayout. CAN PLEASE somebody explain this effect or propose a better solution (I would like to use WRAP_CONTENT instead of an absolute value) ? – Anne Droid Aug 22 '12 at 20:58
  • This is not an explanation, but have you tried using as parent a ScrollView like I also suggested in the comment? At least, I have in my app a ScrollView with a LinearLayout-child with height and width set to wrap_content. The content is added dynamically to the LinearLayout. And it's not clipped. – User Aug 22 '12 at 21:22
  • 1
    Yes, WRAP_CONTENT works in a ScrollView. But I try to write my own scroll view class, because I have to customize it. Unfortunately the original code of Android's ScrollView class didn't helped me to understand, why the views are (not) clipped... – Anne Droid Aug 22 '12 at 23:30
  • 1
    Anne Droid, have you figured out how to invalidate that child's area while using WRAP_CONTENT? I've also run in the same issue and sources didn't help me neither. – vir us Dec 28 '13 at 14:45
  • You should consider subclassing ScrollView instead of recreating your own. What kind of customizations to you need? – clemp6r Mar 15 '14 at 10:19

1 Answers1

-1

You can use parentLayout as ScrollView instead of RelativeLayout and keep childLayout as RelativeLayout.

Bun
  • 1,465
  • 10
  • 23
AMT
  • 136
  • 5
  • Ideally when posting an answer try to solve the poster's problem with a working code sample. It will help the person understand your solution. – Bun Aug 28 '14 at 12:58