2012-05-08 11 views
15

मैं क्षैतिज स्क्रॉलव्यू की स्थिति सेट करने की कोशिश कर रहा हूं, इसलिए यह धक्का दिया गया बटन से मेल खाता है। मैंने इसे असफल तरीके से सेट करने के लिए इसका उपयोग करने का प्रयास किया है:हॉर्टिज़ोंटल स्क्रॉलव्यू की एंड्रॉइड सेटिंग स्थिति

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> 

तो 5 वीं बटन धकेल दिया जाता है, तो (जो गुप्त है) नई गतिविधियों है कि शुरू होता है मैं नया दृश्य सेट करें कि क्षैतिज scrollview शुरू बनाम सही करने के लिए सभी तरह से है चाहते हैं जब बाईं ओर सभी तरह से।

मैं क्षैतिज स्क्रॉलव्यू की स्थिति कैसे सेट कर सकता हूं?

उत्तर

31

अभी आप बटन की स्थिति के बजाय क्षैतिजस्कोलव्यू के ऊपरी-बाएं कोने तक स्क्रॉल करने का प्रयास कर रहे हैं। इस तरह बटन के (एक्स, वाई) की स्थिति को स्क्रॉल का प्रयास करें:

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

संपादित करें:

इस कोड को व्यवहार नहीं होगा के रूप में आप अगर यह 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); 
} 

हालांकि, इस समारोह, हर बार गतिविधि लाभ या फोकस खो देता है कहा जाता है ताकि आप अधिक बार स्क्रॉल स्थिति को अद्यतन करने के अंत हो सकता है आपके इरादे से

क्षैतिज स्क्रॉलव्यू को उप-वर्गीकृत करने के लिए एक और अधिक सुरुचिपूर्ण समाधान होगा और onMeasure() में स्क्रॉल स्थिति सेट करें, जब आप जानते हैं कि दृश्य प्रारंभ किया गया है। ऐसा करने के लिए, मैं दो फ़ाइलों में अपने लेआउट विभाजित है और एक नया वर्ग नामित 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()); 
    } 
} 

जब MyHorizontalScrollView बनाई गई है, यह स्वतः ही अनावश्यक रूप से बढ़ा और बटन लेआउट कहते हैं। फिर सुपर onMeasure() पर कॉल करने के बाद (ताकि यह पता चल सके कि लेआउट प्रारंभ करना समाप्त हो गया है) यह स्क्रॉल स्थिति सेट करता है।

यह नया main.xml है। इसमें केवल नया MyHorizontalScrollView शामिल है, हालांकि आप आसानी से इसे रैखिक या सापेक्ष लेआउट के अंदर डाल सकते हैं और अन्य दृश्य तत्व जोड़ सकते हैं। (आप पैकेज के नाम पर जहां MyHorizontalScrollView स्थित है साथ 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" /> 

और यह बटन है।XML लेआउट कि स्वचालित रूप से 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

मैं के बाद मैं लेआउट सेट लेकिन कुछ नहीं होता इस OnCreate सही में डालने की कोशिश की है। कोई अन्य विचार? – Nick

+0

मैंने अपना जवाब संपादित कर लिया है और समाधान की पूरी व्याख्या जोड़ दी है। – theisenp

+1

जीनियस! उत्कृष्ट उत्तर के लिए धन्यवाद। – 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

संबंधित मुद्दे