2012-05-08 18 views
15

Tôi đang cố gắng đặt vị trí của scrollview ngang sao cho nó tương ứng với nút được đẩy. Tôi đã cố gắng sử dụng này để thiết lập nó không thành công:Vị trí thiết lập Android của scrollbox hortizontal

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

Điều này dẫn đến không có gì, các scrollview không bị ảnh hưởng. Các 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> 

Vì vậy, nếu nút thứ 5 được đẩy (đó là offscreen) khi hoạt động mới bắt đầu tôi muốn thiết lập các quan điểm mới để các scrollview ngang là tất cả các cách ở bên phải so với bắt đầu tất cả các con đường bên trái.

Làm cách nào để đặt vị trí của chế độ xem cuộn ngang?

Trả lời

31

Ngay bây giờ bạn đang cố gắng cuộn đến góc trên cùng bên trái của HorizontalScrollView thay vì vị trí của nút. Hãy thử di chuyển đến (x, y) vị trí của các nút như thế nà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:

Mã này sẽ không cư xử như bạn mong chờ nếu nó được đặt trong onCreate(). Mặc dù bạn đã gọi số setContentView(), bố cục chưa được đo và khởi tạo. Điều này có nghĩa là các phương pháp getLeft()getTop()will both return 0. Cố gắng đặt vị trí cuộn trước khi bố cục được khởi tạo hoàn toàn không có hiệu lực, vì vậy bạn cần gọi số hsv.scrollTo() một thời gian sau onCreate().

Một lựa chọn mà dường như để làm việc được đặt mã trong 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); 
} 

Tuy nhiên, chức năng này được gọi là mỗi lần tăng Hoạt động hoặc mất tập trung, vì vậy bạn có thể kết thúc cập nhật vị trí cuộn thường xuyên hơn hơn bạn dự định.

Một giải pháp thanh lịch hơn sẽ là phân lớp HorizontalScrollView và đặt vị trí cuộn trong onMeasure(), sau khi bạn biết rằng chế độ xem đã được khởi tạo. Để làm điều này, tôi chia bố cục của bạn thành hai tập tin và thêm một lớp mới có tên MyHorizontalScrollView:

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()); 
    } 
} 

Khi MyHorizontalScrollView được tạo ra, nó sẽ tự động thổi phồng và cho biết thêm cách bố trí nút. Sau đó, sau khi gọi siêu onMeasure() (để nó biết bố cục đã hoàn tất khởi tạo), nó sẽ đặt vị trí cuộn.

Đây là tệp main.xml mới. Nó chỉ chứa MyHorizontalScrollView mới, mặc dù bạn có thể dễ dàng đặt nó bên trong một layout tuyến tính hoặc tương đối và thêm các phần tử khung nhìn khác. (Bạn sẽ thay thế com.theisenp.test với tên của gói nơi MyHorizontalScrollView nằm):

<?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" /> 

Và đây là các nút.bố trí xml cũng được thổi phồng bởi MyHorizontalScrollView:

<?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

Tôi đã thử cài đặt này ngay sau khi tôi đặt bố cục nhưng không có gì xảy ra. Bất kỳ ý tưởng nào khác? – Nick

+0

Tôi đã chỉnh sửa câu trả lời của mình và thêm giải thích đầy đủ về giải pháp. – theisenp

+1

Genius! Cảm ơn câu trả lời tuyệt vời. – Nick

6

Mặc dù, đây là một câu hỏi cũ, tôi vừa chạy vào cùng một vấn đề và tìm thấy giải pháp được nêu khác nhưng khá ít phức tạp.

Giả sử file bố trí được đưa ra trong câu hỏi ban đầu và chúng ta hãy giả định rằng nếu một hoạt động được bắt đầu với việc bố trí nhất định, scrollview ngang cần được cuộn vào nút 5.

Để đạt được di chuyển đến nút 5, hãy đặt đoạn mã sau vào phương thức onCreate() của hoạt động:

@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

Đây là một giải pháp đơn giản hơn nhiều. – lintmachine