布局动画问题 - 没有平滑滚动

时间:2015-04-20 12:32:03

标签: android android-listview android-animation

enter image description here

我有一个线性的(A),有几个textview,图像等和scrollview(B)与listview。列表视图的Occroll我想降低图像的高度。当用户进入列表时,高度应减小到图像大小的一半。当用户向上滚动列表时,我想将高度增加到原始高度。

我已设法使用以下代码

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

我面临的问题是,频繁调用requestLayout()会使scrollview滚动得非常缓慢而且不稳定。如何改进实现以使滚动更流畅?

2 个答案:

答案 0 :(得分:1)

请参考此示例。 Not Boaring Actionbar

答案 1 :(得分:-1)

从开发人员文档中,提到不太频繁地致电requestLayout()from Here

  

另一个非常昂贵的操作是遍历布局。每当视图调用requestLayout()时, Android UI系统都需要遍历整个视图层次结构,以找出每个视图需要的大小。如果发现有冲突的测量,则可能需要多次遍历层次结构。

代码的替代方案是:

  1. 获取布局参数getLayoutParams()

  2. 修改参数lp..height = setHeight;

  3. 将布局参数设置为线性布局setLayoutParams(lp);

  4. 代码如下:

     if (scrolledUp) { 
          int height = linearLayout.getMeasuredHeight();  
          int setHeight = height - scrollValue; 
          LinearLayout.LayoutParams lp = linearLayout.getLayoutParams();
          lp.height = setHeight;
          linearLayout.setLayoutParams(lp);
     }