2011-03-01 4 views

답변

3

onCreate, onStart 또는 onResume 중에는 smoothScrolTo()를 호출 할 수 없습니다. 이런 약간의 지연이 제공하려고 :

public void onResume() { 
    super.onResume(); 
    new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      HorizontalScrollView sv = (HorizontalScrollView)findViewById(R.id.ScrollView01); 
      sv.smoothScrollTo(1000, 0); 
     } 
    }, 100); 
} 

이 나를 위해 일을하지만, smoothScrollTo을 호출하는 사람이 더 나은 시간을 알고 복용량 (예를 들어, 청취자에 있습니다.)?

10

View.getViewTreeObserver.addOnGlobalLayoutListener을 사용하여 scrollview가 준비되었을 때 알 수 있습니다. 콜백에서 스크롤을 설정할 수 있습니다.

콜백에서 removeGlobalOnLayoutListener (this)를 사용하여 추가 이벤트 등록을 취소합니다.

scroll.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){ 
    @Override 
    public void onGlobalLayout(){ 
     scroll.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
     scroll.scrollTo(x, y); 
    } 
}); 
0

이 문제를 해결할 수있는 또 다른 방법은 xml을 사용하는 것입니다.

트릭은 HorizontalScrollView의 차일으로 "스페이스 전망"을 추가하고 당신이 원하는 오프셋에의 폭을 설정합니다.

예 : 나는 16dp "여유"를 원하는 예에서

<!--BUTTONS ON HORIZONAL SCROLL --> 
    <HorizontalScrollView 
     android:id="@+id/scroll_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <LinearLayout 
      android:id="@+id/scroll_view_child_layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <!-- This View does the trick! --> 
      <Space 
       android:layout_width="16dp" 
       android:layout_height="match_parent" /> 

      <Button 
       android:id="@+id/btn_1" 
       style="@style/HorizontalScrollButtons" 
       android:text="Btn1" /> 

      <Button 
       android:id="@+id/btn_2" 
       style="@style/HorizontalScrollButtons" 
       android:text="Btn1" /> 

      <!-- Keep adding buttons... --> 

      <!-- This View does the trick too! --> 
      <Space 
       android:layout_width="16dp" 
       android:layout_height="match_parent" /> 


     </LinearLayout> 

    </HorizontalScrollView> 

그래서 공간보기 16dp의 에게 제공합니다. 대신이의 The way we want

그것은이를 줄 것이다

... ... ... enter image description here

을 ...보기를 시작으로.

관련 문제