0

enter image description here

I have a linearlyaout(A) with couple of textviews,image etc and scrollview (B) with a listview. Onscroll of the list view I would like to reduce the height of the image. The height should be reduced to half the size of the image when the user goes down the list. When the user scrolls up the list, I would like to increase the height to the original height.

I have managed to get it to work using the following code

 if (scrolledUp) { 
              int height = linearLayout.getMeasuredHeight();  
              int setHeight = height - scrollValue; 
              linearLayout.getLayoutParams().height = setHeight;
              linearLayout.requestLayout();
            }

The problem I am facing is, the frequent calls to requestLayout() is making the scrollview scrolling horribly slow and shuddery. How can I improve the implementation to make the scroll smoother?

rfsk2010
  • 8,571
  • 4
  • 32
  • 46
  • If you dont want to go with using a lib, id suggest using [this](http://developer.android.com/tools/debugging/debugging-tracing.html) and see where your code falters! – Skynet Apr 20 '15 at 12:42
  • Have you tried moving the position of both A & B instead of doing relayout? – Kai Apr 20 '15 at 12:49

2 Answers2

1

Please refer this example. Not Boaring Actionbar

Jigar Shekh
  • 2,800
  • 6
  • 29
  • 54
-1

From Developer documentation, it is mentioned to call requestLayout() less frequently : from Here

Another very expensive operation is traversing layouts. Any time a view calls requestLayout(), the Android UI system needs to traverse the entire view hierarchy to find out how big each view needs to be. If it finds conflicting measurements, it may need to traverse the hierarchy multiple times.

The alternative for your code is :

  1. get layout parameters getLayoutParams()

  2. modify the parameter lp..height = setHeight;

  3. set layout parameters to linear layout setLayoutParams(lp);

Code looks like :

 if (scrolledUp) { 
      int height = linearLayout.getMeasuredHeight();  
      int setHeight = height - scrollValue; 
      LinearLayout.LayoutParams lp = linearLayout.getLayoutParams();
      lp.height = setHeight;
      linearLayout.setLayoutParams(lp);
 }
Kushal
  • 8,100
  • 9
  • 63
  • 82
  • setLayoutParams calls requestLayout() internally – rfsk2010 Apr 20 '15 at 15:19
  • You are right.. `setLayoutParams` calls `requestLayout` but it is good practice to call `requestLayout` after setting layout parameters.. reference http://stackoverflow.com/a/26398231/1994950 – Kushal Apr 21 '15 at 03:58