2013-03-05 50 views
5

貌似我無法獲取或設置在我簡單的佈局的任何位置或滾動量:獲取孩子的位置,滾動到它

// Relative layout - main_layout 
<HorizontalScrollView 
    android:id="@+id/MenuScroll" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:fillViewport="true" 
    android:scrollbars="none" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="48dp" 
     android:orientation="horizontal" 
     android:layout_gravity="center" 
     android:padding="0dp" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="150dp" 
      android:layout_height="fill_parent" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="150dp" 
      android:layout_height="fill_parent" /> 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="150dp" 
      android:layout_height="fill_parent" /> 

     <Button 
      android:id="@+id/button4" 
      android:layout_width="150dp" 
      android:layout_height="fill_parent" /> 

     <Button 
      android:id="@+id/button5" 
      android:layout_width="150dp" 
      android:layout_height="fill_parent" /> 

    </LinearLayout> 
</HorizontalScrollView> 
// End of RelativeLayout - main_layout 

我想要什麼,我MainActivity是得到的位置HorizontalScrollView中的任何按鈕並滾動到該位置。目前我無法獲得左側值(始終爲0),如果我嘗試使用scrollTo,HorizontalScrollView,它就不會,無論x或y的值是多少。

// imports 
public class MainActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main_layout); 

     final Button button3 = (Button)findViewById(R.id.button3); 
     Rect rectf = new Rect(); 
     button3.getLocalVisibleRect(rectf); 
     Log.i("Left: ", String.valueOf(rectf.left)); // it is always 0 

     HorizontalScrollView MenuScroll = (HorizontalScrollView)findViewById(R.id.MenuScroll); 
     // Trying to scroll to X=100 but it doesn't 
     MenuScroll.scrollTo(100, MenuScroll.getBottom()); 
    } 
} 

所以,正如我所說,我不能讓左側位置對我的按鈕,或者可以滾動到一些X位置在我HorizontalScrollView。這可能嗎?

回答

11

(它總是0)

記住的意見沒有在onCreate方法尺寸,因爲它們的onCreate回調返回後,放置在屏幕上。

我想在我的MainActivity中獲取Horizo​​ntalScrollView中任何按鈕 的位置並滾動到該位置。

看一看下面的代碼(放在onCreate法):

final HorizontalScrollView MenuScroll = (HorizontalScrollView) findViewById(R.id.MenuScroll); 

    MenuScroll.post(new Runnable() { 

     @Override 
     public void run() { 
      final Button button3 = (Button) findViewById(R.id.button3); 
      int scrollTo = 0; 
      final int count = ((LinearLayout) MenuScroll.getChildAt(0)) 
        .getChildCount(); 
      for (int i = 0; i < count; i++) { 
       final View child = ((LinearLayout) MenuScroll.getChildAt(0)) 
         .getChildAt(i);     
       if (child != button3) { 
        scrollTo += child.getWidth(); 
       } else { 
        break; 
       } 
      } 
      MenuScroll.scrollTo(scrollTo, 0); 
     } 

    }); 

另外要小心你在佈局中使用的屬性(我指的是layout_gravity可能導致問題,如果我沒有錯)。

+0

無需遍歷所有孩子的意見,增加了他們的寬度來獲得滾動值。只需設置'scrollTo = button3.getLeft()' – camelCaseCoder 2015-12-17 10:41:49

2

可以直接完成,如:

scrollTo = button3.getLeft();