如何以编程方式滚动Horizo​​ntalScrollView

时间:2022-08-24 23:52:37

I have an HorizontalScrollView which contains a RelativeLayout. This layout is empty in the XML, and is populated from java in the onCreate.

我有一个包含RelativeLayout的Horizo​​ntalScrollView。此布局在XML中为空,并在onCreate中从java填充。

I would like this scroll view to be initially somewhere in the middle of the RelativeLayout, which is way larger than the screen.

我希望这个滚动视图最初位于RelativeLayout中间的某个位置,它比屏幕大。

I tried mHorizScrollView.scrollTo(offsetX, 0); which doesn't work. I don't know what's wrong with this.

我试过mHorizScrollView.scrollTo(offsetX,0);这不起作用。我不知道这有什么问题。

I could post the code, but it is not really relevant. What matters is that everything is done programatically (has to :s), and that the initial position of the HorizontalScrollView has to be set programmatically.

我可以发布代码,但它并不真正相关。重要的是,所有内容都以编程方式完成(必须:s),并且必须以编程方式设置Horizo​​ntalScrollView的初始位置。

Thanks for reading. Please tell me if you need more details or if this is not clear enough.

谢谢阅读。请告诉我您是否需要更多详细信息,或者这是否不够清楚。

8 个解决方案

#1


26  

I found that if you extend the HorizontalScrollView and override the onLayout, you can cleanly scroll, after the super call to onLayout:

我发现如果你扩展Horizo​​ntalScrollView并覆盖onLayout,你可以在超级调用onLayout之后干净地滚动:

MyScrollView extends HorizontalScrollView {
    protected void onLayout (boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        this.scrollTo(wherever, 0);
    }
}

EDIT: For scrolling to start or end you can use:

编辑:要滚动到开始或结束,您可以使用:

this.fullScroll(HorizontalScrollView.FOCUS_RIGHT/FOCUS_LEFT);

instead of

this.scrollTo(wherever, 0);

#2


24  

public void autoSmoothScroll() {

        final HorizontalScrollView hsv = (HorizontalScrollView) view.findViewById(R.id.horiscroll);
        hsv.postDelayed(new Runnable() {
            @Override
            public void run() {
                //hsv.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
                hsv.smoothScrollBy(500, 0);
            }
        },100);
    }

#3


15  

To test whether it's a timing issue (which I think it is), instead of calling scrollTo() in onStart, call postDelayed() with a Runnable that calls scrollTo, with a delay of 30 or so.

要测试它是否是计时问题(我认为是这样),而不是在onStart中调用scrollTo(),而是使用调用scrollTo的Runnable调用postDelayed(),延迟为30左右。

#4


9  

A better approach would be using the ViewTreeObserver to observe layouts.

更好的方法是使用ViewTreeObserver观察布局。

View interestedInView;
onCreate(){
  //Code

  //Observe for a layout change
  ViewTreeObserver viewTreeObserver = interestedInView.getViewTreeObserver();
  if (viewTreeObserver.isAlive()) {
    viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
        //REMOVE THE VIEWTREE OBSERVER
        //interestedInView is ready for size and position queries because it has been laid out
      }
    });
  }
}

#5


1  

The problem is that the view doesn't have a size yet. The Clean way to do this is to implement the onSizeChanged of the ScrollView to send a message to a Handler in your activity to in order to notify the activity that the view has a size and that scrolling is possible.

问题是视图还没有大小。 Clean的方法是实现ScrollView的onSizeChanged,以便向活动中的Handler发送消息,以便通知活动视图具有大小并且可以滚动。

#6


1  

Use view.post and write scrollTo(x,y) inside run method. This the way to invalidate view in post method as per android API document.

使用view.post并在run方法中编写scrollTo(x,y)。这是根据android API文档使post方法中的视图无效的方法。

#7


0  

The scrollTo function should work. What is the layout_width of the HSV, and the view inside of it?

scrollTo函数应该可以工作。什么是HSV的layout_width,以及它内部的视图?

#8


0  

The onSizeChanged function should work. What you fail to understand is that the onSizeChanged should send a message to a Handler of your activity and not any callback function, because a handler will execute code in the UI thread. You cannot do UI operations outside it. Have a look at handlers.

onSizeChanged函数应该可以工作。您无法理解的是onSizeChanged应该向您的活动的Handler发送消息而不是任何回调函数,因为处理程序将在UI线程中执行代码。你不能在它之外做UI操作。看看处理程序。

#1


26  

I found that if you extend the HorizontalScrollView and override the onLayout, you can cleanly scroll, after the super call to onLayout:

我发现如果你扩展Horizo​​ntalScrollView并覆盖onLayout,你可以在超级调用onLayout之后干净地滚动:

MyScrollView extends HorizontalScrollView {
    protected void onLayout (boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        this.scrollTo(wherever, 0);
    }
}

EDIT: For scrolling to start or end you can use:

编辑:要滚动到开始或结束,您可以使用:

this.fullScroll(HorizontalScrollView.FOCUS_RIGHT/FOCUS_LEFT);

instead of

this.scrollTo(wherever, 0);

#2


24  

public void autoSmoothScroll() {

        final HorizontalScrollView hsv = (HorizontalScrollView) view.findViewById(R.id.horiscroll);
        hsv.postDelayed(new Runnable() {
            @Override
            public void run() {
                //hsv.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
                hsv.smoothScrollBy(500, 0);
            }
        },100);
    }

#3


15  

To test whether it's a timing issue (which I think it is), instead of calling scrollTo() in onStart, call postDelayed() with a Runnable that calls scrollTo, with a delay of 30 or so.

要测试它是否是计时问题(我认为是这样),而不是在onStart中调用scrollTo(),而是使用调用scrollTo的Runnable调用postDelayed(),延迟为30左右。

#4


9  

A better approach would be using the ViewTreeObserver to observe layouts.

更好的方法是使用ViewTreeObserver观察布局。

View interestedInView;
onCreate(){
  //Code

  //Observe for a layout change
  ViewTreeObserver viewTreeObserver = interestedInView.getViewTreeObserver();
  if (viewTreeObserver.isAlive()) {
    viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
        //REMOVE THE VIEWTREE OBSERVER
        //interestedInView is ready for size and position queries because it has been laid out
      }
    });
  }
}

#5


1  

The problem is that the view doesn't have a size yet. The Clean way to do this is to implement the onSizeChanged of the ScrollView to send a message to a Handler in your activity to in order to notify the activity that the view has a size and that scrolling is possible.

问题是视图还没有大小。 Clean的方法是实现ScrollView的onSizeChanged,以便向活动中的Handler发送消息,以便通知活动视图具有大小并且可以滚动。

#6


1  

Use view.post and write scrollTo(x,y) inside run method. This the way to invalidate view in post method as per android API document.

使用view.post并在run方法中编写scrollTo(x,y)。这是根据android API文档使post方法中的视图无效的方法。

#7


0  

The scrollTo function should work. What is the layout_width of the HSV, and the view inside of it?

scrollTo函数应该可以工作。什么是HSV的layout_width,以及它内部的视图?

#8


0  

The onSizeChanged function should work. What you fail to understand is that the onSizeChanged should send a message to a Handler of your activity and not any callback function, because a handler will execute code in the UI thread. You cannot do UI operations outside it. Have a look at handlers.

onSizeChanged函数应该可以工作。您无法理解的是onSizeChanged应该向您的活动的Handler发送消息而不是任何回调函数,因为处理程序将在UI线程中执行代码。你不能在它之外做UI操作。看看处理程序。