2012-05-08 9 views
15

水平スクロールビューの位置をプッシュしたボタンに対応させようとしています。私はこれをうまく設定できませんでした:水平スクロールビューのAndroidの設定位置

HorizontalScrollView hsv = (HorizontalScrollView)findViewById(R.id.ScrollView); 
int x, y; 
x = hsv.getLeft(); 
y = hsv.getTop(); 
hsv.scrollTo(x, y); 

これは何も起こりません、スクロールビューは影響を受けません。 のxml:

だから、
<HorizontalScrollView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/ScrollView" 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:layout_alignParentBottom="true" 
     android:background="@null" 
     android:scrollbars="none" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="horizontal" > 

      <Button 
       android:layout_width="100dp" 
       android:layout_height="fill_parent" 
       android:layout_marginBottom="-5dp" 
       android:text="btn0" 
       android:id="@+id/btn0" 
       android:background="@drawable/yellow_btn" /> 

      <Button 
       android:layout_width="100dp" 
       android:layout_height="fill_parent" 
       android:layout_marginBottom="-5dp" 
       android:background="@drawable/yellow_btn" 
       android:text="bnt1" 
       android:id="@+id/btn1" /> 

      <Button 
       android:layout_width="100dp" 
       android:layout_height="fill_parent" 
       android:layout_marginBottom="-5dp" 
       android:background="@drawable/yellow_btn" 
       android:text="btn2" 
       android:id="@+id/btn2" /> 

      <Button 
       android:layout_width="100dp" 
       android:layout_height="fill_parent" 
       android:layout_marginBottom="-5dp" 
       android:background="@drawable/yellow_btn" 
       android:text="btn3" 
       android:id="@+id/btn3" /> 

     <Button 
       android:layout_width="100dp" 
       android:layout_height="fill_parent" 
       android:layout_marginBottom="-5dp" 
       android:background="@drawable/yellow_btn" 
       android:text="btn4" 
       android:id="@+id/btn4" /> 

     <Button 
       android:layout_width="100dp" 
       android:layout_height="fill_parent" 
       android:layout_marginBottom="-5dp" 
       android:background="@drawable/yellow_btn" 
       android:text="btn5" 
       android:id="@+id/btn5" /> 

     </LinearLayout> 
    </HorizontalScrollView> 

第五ボタンが(オフスクリーンである)押された場合、私は水平scrollviewが出始め対右にすべての方法であるように、新しいビューを設定する新しい活動を開始左いっぱい。

水平スクロールビューの位置を設定するにはどうすればよいですか?

答えて

31

今は、ボタンの位置ではなく、Horizo​​ntalScrollViewの左上隅にスクロールしようとしています。このようなボタンの位置(x、y)までスクロールしてみてください。

HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.ScrollView); 
Button button = (Button) findViewById(R.id.btn5); 
int x, y; 
x = button.getLeft(); 
y = button.getTop(); 
hsv.scrollTo(x, y); 

EDIT:それはonCreate()に配置されている場合、あなたが期待するよう

このコードは動作しません。 setContentView()と呼んでも、レイアウトはまだ測定されていません。これは、getLeft()getTop()の方法will both return 0を意味します。レイアウトが完全に初期化される前にスクロール位置を設定しようとしても効果がないので、onCreate()の後にhsv.scrollTo()と呼ぶ必要があります。 onWindowFocusChanged()にコードを配置されて動作するようです

一つのオプション:

あなたはより頻繁にスクロール位置を更新するに終わるかもしれないので、しかし、この関数は、活動の利益またはがフォーカスを失うたびに呼び出され
@Override 
public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 

    HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.ScrollView); 
    Button button = (Button) findViewById(R.id.btn5); 
    int x, y; 
    x = button.getLeft(); 
    y = button.getTop(); 
    hsv.scrollTo(x, y); 
} 

あなたが意図したよりも。

より洗練されたソリューションは、Horizo​​ntalScrollViewをサブクラス化し、ビューが初期化されたことを知った後にonMeasure()にスクロール位置を設定することです。これを行うには、私は2つのファイルにレイアウトを分割し、新しいクラスの名前MyHorizo​​ntalScrollView追加:MyHorizo​​ntalScrollViewが作成されると

package com.theisenp.test; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.HorizontalScrollView; 

public class MyHorizontalScrollView extends HorizontalScrollView { 

    public MyHorizontalScrollView(Context context) { 
     super(context); 
     addButtons(context); 
    } 

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

    /** 
    * Inflates the layout containing the buttons and adds them to the ScrollView 
    * @param context 
    */ 
    private void addButtons(Context context) { 
     View buttons = inflate(context, R.layout.buttons, null); 
     addView(buttons); 

    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     //Find button 5 and scroll to its location 
     View button = findViewById(R.id.btn5); 
     scrollTo(button.getLeft(), button.getTop()); 
    } 
} 

を、それが自動的に膨張し、ボタンのレイアウトを追加します。その後、スーパーonMeasure()を呼び出すと(レイアウトが初期化を完了したことがわかるように)、スクロール位置が設定されます。

これは新しいmain.xmlです。新しいMyHorizo​​ntalScrollViewのみが含まれていますが、LinearまたはRelativeレイアウト内に簡単に配置して他のビュー要素を追加することもできます。 (あなたはMyHorizo​​ntalScrollViewが配置されているパッケージの名前とcom.theisenp.testに代わる):

<?xml version="1.0" encoding="utf-8"?> 
<com.theisenp.test.MyHorizontalScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/ScrollView" 
android:layout_width="fill_parent" 
android:layout_height="50dp" 
android:layout_alignParentBottom="true" 
android:background="@null" 
android:scrollbars="none" /> 

そして、これはボタンです。自動的にMyHorizo​​ntalScrollViewによって膨張されたXMLレイアウト:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="fill_parent" 
android:orientation="horizontal" > 

    <Button 
    android:id="@+id/btn0" 
    android:layout_width="100dp" 
    android:layout_height="fill_parent" 
    android:layout_marginBottom="-5dp" 
    android:text="btn0" /> 

    <Button 
    android:id="@+id/btn1" 
    android:layout_width="100dp" 
    android:layout_height="fill_parent" 
    android:layout_marginBottom="-5dp" 
    android:text="bnt1" /> 

    <Button 
    android:id="@+id/btn2" 
    android:layout_width="100dp" 
    android:layout_height="fill_parent" 
    android:layout_marginBottom="-5dp" 
    android:text="btn2" /> 

    <Button 
    android:id="@+id/btn3" 
    android:layout_width="100dp" 
    android:layout_height="fill_parent" 
    android:layout_marginBottom="-5dp" 
    android:text="btn3" /> 

    <Button 
    android:id="@+id/btn4" 
    android:layout_width="100dp" 
    android:layout_height="fill_parent" 
    android:layout_marginBottom="-5dp" 
    android:text="btn4" /> 

    <Button 
    android:id="@+id/btn5" 
    android:layout_width="100dp" 
    android:layout_height="fill_parent" 
    android:layout_marginBottom="-5dp" 
    android:text="btn5" /> 

</LinearLayout> 
+0

私はレイアウトを設定した後、私は、これはOnCreateの右に入れて試してみたが、何も起こりません。他のアイデア? – Nick

+0

私は自分の答えを編集し、解決策の完全な説明を追加しました。 – theisenp

+1

Genius!優れた答えをありがとう。 – Nick

6

が、これは私が最近、同じ問題に遭遇し、さらに別のではなく、より複雑で解決策を見つけ、古い質問です。

は、のは、元の質問に与えられたレイアウトファイルを想定してみましょうとの活動は、指定されたレイアウトで開始された場合、水平方向のscrollviewがボタン5にスクロールを実現するためにボタン5.

にスクロールする必要があることを想定してみましょう活動のonCreate()方法に次のコードを置く:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Some code such as layout inflation. 

     final HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.ScrollView); 

     // Scrolling to button 5. 
     hsv.post(new Runnable() { 
      @Override 
      public void run() { 
       // Get the button. 
       View button = findViewById(R.id.btn5); 

       // Locate the button. 
       int x, y; 
       x = button.getLeft(); 
       y = button.getTop(); 

       // Scroll to the button. 
       hsv.scrollTo(x, y); 
      } 
     }); 
    } 
} 
+0

これははるかに簡単な解決策です。 – lintmachine

関連する問題