【问题标题】:Changing View Height inside a custom ScrollView onScrollChanged在自定义 ScrollView onScrollChanged 中更改视图高度
【发布时间】:2014-09-30 15:47:47
【问题描述】:

我正在尝试制作以下效果:

我有一个自定义 ScrollView(为了获取 onScrollChanged 侦听器)和一个自定义 View。在自定义视图中,我成功地根据需要放置项目。

这是我的自定义视图:

public class CustomView extends FrameLayout {

private TextView nameView;
private TextView emailView;
private ImageView addressView;
private Tracks track ;
private double scrollProgress = 0.0;
private double topViewScaleFactor = 2.0;
private double collapsedViewHeight = 200.0;
private double expandedViewHeight = 700.0; 
private double scrollProgressPerView = expandedViewHeight;

View v;
View firstItem;
View secondView;


          int itemMinHeight = 200;
          int itemMaxHeight = 700;

public CustomView(MyScrollView paramEventListView, Context paramContext){

           super(paramContext);
}        



public CustomView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub

}


public CustomView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub

}


public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub

}

    @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    // TODO Auto-generated method stub
    int m = getMeasuredWidth();


         int itemWidth = (r-l)/getChildCount();
       //  int itemHeight = (b-t)/getChildCount();


         firstItem = getChildAt(0);
        //firstItem.layout(0, 0, r-l, itemMaxHeight);
         firstItem.measure(View.MeasureSpec.makeMeasureSpec(m, MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(itemMaxHeight, MeasureSpec.EXACTLY));
         firstItem.layout(0, 0, r-l, itemMaxHeight);

         secondView = getChildAt(1);
         secondView.measure(View.MeasureSpec.makeMeasureSpec(m, MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(itemMinHeight, MeasureSpec.EXACTLY));
         secondView.layout(0, itemMaxHeight, r-l, itemMinHeight+itemMaxHeight);

         int FirstAndSEcondItemHeight = firstItem.getHeight() + secondView.getHeight();
         for(int i=2; i< this.getChildCount(); i++){
              v = getChildAt(i);
          //  v.layout(itemWidth*i, 0, (i+1)*itemWidth, b-t); 

             v.layout(0, FirstAndSEcondItemHeight + (itemMinHeight*(i-2)), r-l, FirstAndSEcondItemHeight + ((i-1)*itemMinHeight)); 
         }


}


    @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);



    int heightMeasured = 0;
    /*for each child get height and
    heightMeasured += childHeight;*/
     for(int i=0; i< this.getChildCount(); i++){
         heightMeasured += getChildAt(i).getHeight();
     }

    //If I am in a scrollview i got heightmeasurespec == 0, so
    if(heightMeasureSpec == 0){
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightMeasured, MeasureSpec.EXACTLY);
    }

    setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(this.getSuggestedMinimumHeight(), heightMeasureSpec));
}

这是我的自定义滚动视图:

public class MyScrollView extends ScrollView{



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

}

public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    add(context);
} 

public MyScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void setScrollViewListener(ScrollViewListener scrollViewListener) {
    this.scrollViewListener = scrollViewListener;


}
@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
    super.onScrollChanged(x, y, oldx, oldy);

 // How can I acces to each child in the customView class, and change their height depending on the scrollChanged


}

但现在我需要在滚动滚动视图时更改项目高度。我不知道在 onScrollChanged 中放什么... 如果有人有想法?

谢谢

【问题讨论】:

  • 嘿!我必须实施同样的事情。你能告诉我你最后做了什么吗?
  • 有关信息我看过一个库,但尚未测试:github.com/borjabravo10/…

标签: android android-custom-view custom-scrolling


【解决方案1】:

也许您想通过调用invalidate() 来强制重绘视图?

来自评论:我认为您需要在 onMeasure 方法中移动代码以替换“TODO”存根,以便在您的操作之后调用 super.onMeasure()。并确保将新的宽度/高度计算传递给它。

【讨论】:

  • 也许 :) 我需要将 invalidate() 放在哪里?在 OnScrollChanged 中还是在 onDraw() 的覆盖方法中?
  • Invalidate 本身会调用 onDraw,所以你不希望它在那里。我建议在 onScrollChanged 中执行此操作。当然这是假设视图在重绘时会重新计算每个孩子的高度。
  • 我正在这样做:查看第二个 = custoView.getChildAt(1); custoView.setChildHeight(秒, (int)(MIN_ITEM_ROW +1.5*100)); custoView.invalidate();孩子的高度没有变化......但是就像我的customView的onLayout()一样,我在放置孩子时设置了默认项目高度,当调用invalidate()时,它会从onlayout()重新创建孩子并设置再次项目默认高度?还是我错过了什么?
  • 实际上我成功改变了一个项目视图的高度;但是视图组(包含所有项目),我不能改变的是高度,即使他的一个孩子的身高比 biginning 大,它也不会移动......我需要在 onSizeChanged(..) ?
  • 我认为您需要在 onMeasure 方法中移动代码以替换“TODO”存根,以便在您的操作之后调用 super.onMeasure()。并确保将新的宽度/高度计算传递给它。
猜你喜欢
  • 2012-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-22
相关资源
最近更新 更多