92 votes

Pourquoi mon activité Android commence-t-elle toujours par défiler vers le bas ?

Chaque fois que je commence cette activité, elle démarre toujours en bas de l'écran, c'est-à-dire qu'elle défile jusqu'en bas. Je ne fais rien de bizarre dans l'activité OnCreate (ou ailleurs d'ailleurs) qui pourrait modifier la position de défilement. J'ai essayé de mettre l'accent sur le contrôle focalisable le plus haut, et la méthode scrollto, mais aucune de ces méthodes ne fonctionne. Par ailleurs, aucune de mes autres activités ne présente ce problème. Voici la mise en page :

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edit_refresh_update_header"
            android:textSize="18sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Variable:"
            android:textSize="18sp"/>

        <Spinner
            android:id="@+id/edit_refresh_variables_spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Network:"
            android:textSize="18sp"/>

        <RadioGroup
            android:id="@+id/widget1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical">

            <RadioButton
                android:text="Web"
                android:id="@+id/edit_refresh_update_rb_web"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="NetRadioButtonSelected"
                android:checked="true"/>

            <RadioButton
                android:text="Socket Server"
                android:id="@+id/edit_refresh_update_rb_socket_server"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="NetRadioButtonSelected"/>
        </RadioGroup>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Socket server request type:"
            android:textSize="18sp"/>

        <Spinner
            android:id="@+id/edit_refresh_socket_server_req_types_spinner"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Socket server body:"
            android:textSize="18sp"/>

        <EditText
            android:layout_width="match_parent"
            android:id="@+id/edit_refresh_update_ss_body"
            android:layout_height="wrap_content"
            android:enabled="false"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Url:"
            android:textSize="18sp"/>

        <EditText
            android:layout_width="match_parent"
            android:id="@+id/edit_refresh_update_url"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>

        <Button
            android:text="Save refresh update"
            android:id="@+id/edit_refresh_save_btn"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_marginLeft="20dip"
            android:layout_marginRight="20dip"
            android:layout_marginBottom="20dp"
            android:layout_alignParentBottom="true"
            android:onClick="SaveRefreshUpdate">
        </Button>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>
    </LinearLayout>
</ScrollView>

0 votes

Comment avez-vous essayé de changer de cible ?

1 votes

En utilisant la méthode fulscroll comme décrit ci-dessous et la méthode scrollto comme également décrit ci-dessous. Cela ne me dérange pas nécessairement de le réparer de cette façon, mais il doit y avoir une raison pour laquelle il commence comme ça et je préfère le réparer en corrigeant cette raison.

1voto

zapl Points 18808

Je ne sais pas pourquoi mais vous pourriez expérimenter avec

ScrollView sv = (ScrollView) findViewById(R.id.scroll);
sv.scrollTo(0,0);

dans votre onCreate pour le faire défiler manuellement jusqu'à l'endroit où vous souhaitez qu'il soit.

1voto

Mike Points 714

Avez-vous essayé d'utiliser la méthode fullScroll ?http://developer.Android.com/reference/Android/widget/ScrollView.html#fullScroll(int)

yourScrollView.fullScroll(ScrollView.FOCUS_UP);

1voto

mccallbear Points 186

Pour le développement de Xamarin Android, si vous voulez faire la solution @Graeme alors :

// Set Focus to ScrollView
ScrollView scrollView = (ScrollView)FindViewById(Resource.Id.scrollView);
scrollView.FocusableInTouchMode = true;
scrollView.DescendantFocusability = Android.Views.DescendantFocusability.BeforeDescendants;

1voto

codepeaker Points 116

Il suffit de faire en sorte qu'elle ne soit pas focalisable dans onCreate()

editText.setFocusable(false);

et utiliser

editText.setOnClickListener(new ....){
 ......
  editText.setFocusable(true);
  editText.setFocusableInTouchMode(true);
  editText.requestFocus();

  showSoftKeyboard(); //Use InputMethodManager to open soft Keyboard

 ......
 };

et il ne défilera pas vers Edittext la prochaine fois, un fragment ou une activité est créé.

0voto

Amit Points 1161

scrollView.requestFocus(View.FOCUS_UP) a résolu mon problème lorsque je voulais que la vue de dessus soit visible. Spécifier le paramètre de direction à la méthode requestFocus(int direction) est un bon moyen de contrôler le ScrollView position. Plus de détails aquí .

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X