Java 水平滚动视图滚动至';t在视图的原始宽度上方滚动

Java 水平滚动视图滚动至';t在视图的原始宽度上方滚动,java,android,scroll,android-custom-view,horizontalscrollview,Java,Android,Scroll,Android Custom View,Horizontalscrollview,我有一个包含水平滚动视图的自定义视图。滚动视图的宽度是match_parent,其子视图的宽度最初是基于自定义视图的属性值以编程方式设置的。在某些情况下,滚动视图子视图的宽度会以编程方式更新(增加)。问题是,在更新之后,scrollTo方法仍然无法滚动到原始宽度值以上(scrollBy也是如此) 如果相关的话,封闭视图(自定义视图)的左右边距等于屏幕的一半 例如: 初始水平滚动视图的子项宽度:1000 水平滚动视图的父宽度:1080 水平滚动视图的左/右填充=540 新HorizontalSc

我有一个包含水平滚动视图的自定义视图。滚动视图的宽度是match_parent,其子视图的宽度最初是基于自定义视图的属性值以编程方式设置的。在某些情况下,滚动视图子视图的宽度会以编程方式更新(增加)。问题是,在更新之后,scrollTo方法仍然无法滚动到原始宽度值以上(scrollBy也是如此)

如果相关的话,封闭视图(自定义视图)的左右边距等于屏幕的一半

例如:

  • 初始水平滚动视图的子项宽度:1000
  • 水平滚动视图的父宽度:1080
  • 水平滚动视图的左/右填充=540
  • 新HorizontalScrollView的子视图宽度:2000
  • 滚动到(1100,0)=滚动到(1000,0) 问题似乎是,在重新绘制子对象之前,在调用requestLayout之前,滚动会发生,并且只计划在将来某个时候执行视图的更新(invalidate()也是如此)

    我通过在一段时间后执行scrollTo,使用一种变通方法解决了这个问题:

    new Handler().postDelayed(() -> ((Activity) getContext()).runOnUiThread(() -> m_horizontalScrollView.scrollTo(currentScrollX, 0)), 200);
    
    我仍然愿意接受更好的解决方案

    private void update(float timelineLength, float timelineStepWidth, int currentScrollX) {
        m_rulerView = findViewById(R.id.ruler);
        m_rulerView.setIndicator(null);
        m_rulerView.getLayoutParams().width = (int) timelineLength;
        m_rulerView.requestLayout();
    
        m_timelineLayout = findViewById(R.id.timeline);
        m_timelineLayout.getLayoutParams().width = (int) timelineLength;
        m_timelineLayout.requestLayout();
    
        m_horizontalScrollView.scrollTo(currentScrollX, 0);
    }
    
    new Handler().postDelayed(() -> ((Activity) getContext()).runOnUiThread(() -> m_horizontalScrollView.scrollTo(currentScrollX, 0)), 200);