내 Android 활동이 항상 맨 아래로 스크롤되기 시작하는 이유는 무엇입니까?


91

이 활동을 시작할 때마다 항상 맨 아래로 시작하여 맨 아래로 스크롤됩니다. 나는 스크롤 위치를 변경할 것으로 예상되는 OnCreate 활동 (또는 그 문제에 대한 모든 곳)에서 이상한 일을하고 있지 않습니다. 포커스를 최상위 포커스 가능한 컨트롤과 scrollto 메서드로 설정하려고 시도했지만 둘 다 작동하지 않습니다. 게다가 내 다른 활동에는이 문제가 없습니다. 레이아웃은 다음과 같습니다.

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

초점을 정확히 어떻게 바꾸려고 했습니까?
devmiles.com

1
아래에 설명 된대로 fulscroll 메서드와 아래에 설명 된대로 scrollto 메서드를 사용합니다. 이런 식으로 고칠 필요는 없지만 그렇게 시작하는 이유가있을 것 같고 오히려 고쳐서 고치고 싶습니다.
MrGibbage

답변:


306

Android가 활동을 시작하면 일부 제어에 집중해야합니다. 포커스를받을 지정된 컨트롤이 없으면 시스템은 포커스를 원하는 첫 번째 적합한 컨트롤을 선택합니다. 당신이 당신의 LinearLayout에 다음과 같은 속성을 설정하면 - android:focusableInTouchMode="true"당신은 LinearLayout시작에 집중 될 것이며 활동하지 않습니다 스크롤을 EditText아래있다.


내 GridLayout에서 작동하지 않습니다. 레이아웃 중간에 GridView가 있으며 매번 포커스를 얻습니다.
이크발

2
하루 종일 머리를 긁적이며 한 가지 특정 활동에서만 발생하는 이유를 찾을 수 없었고 어떤 관점에도 초점을 맞추지 않았기 때문에 여전히 이해할 수 없습니다. 하지만 당신 덕분에이 문제를 해결할 수있었습니다. 내가 새로운 활동에서 새로운 일이 없기 때문에 누군가가 가능한 이유가 무엇인지에 대해 약간의 빛을 던질 수 있다면 좋을 것입니다.
Shubhral

2
중요한 참고 사항 : 이것은 스크롤 뷰 자체가 아니라 LinearLayout내부에 속성으로 추가 ScrollView됩니다.
Boris Strandjev

@BorisStrandjev 매우 중요한 메모입니다. 답변에 추가해야합니다. 그건 그렇고, 감사합니다 : P
Coreggon

말 그대로 답변에 언급되어 있습니다.
devmiles.com

35

레이아웃의 항목을 오염시키는 대신 다음을 활동 / 클래스에 추가 할 수 있습니다.

    ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
    scrollView.setFocusableInTouchMode(true); 
    scrollView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);

이렇게하면 scrollView 내의 요소가 포커스를 얻는 것이 아니라 첫 번째 포커스를 얻는 컨테이너로서의 scrollView가 그 후 자식 뷰로 이동합니다.


내가있는 ScrollView 안에 아이들을 기반으로 설정 ListView를 높이려고 할 때 부분적으로 나를 위해 작동
콘스탄틴 Konopko

이것은 도움이되었습니다! (XML에서 동일한 속성이 작동하지 않았지만 ..)
마탄 다한에게

매력처럼 작동합니다! Thx
Bogy

그것은 나를 위해 작동했지만 다음 줄을 추가했습니다. scrollView.fullScroll (ScrollView.FOCUS_UP) 둘 다 별도로 작동하지 않았습니다.
Juan Giorello

3

이 코드를 시도해 볼 수 있습니다.

scroller.post(new Runnable() { 
    public void run() { 
        scroller.fullScroll(ScrollView.FOCUS_DOWN); 
    } 
}); 

[N : B] [참조 링크] 1


1
좋은 옵션이지만 개봉 후 멍청이를 시뮬레이트하므로 좋은 사용자 경험이
아니라고

2

devmiles.com이 말한 것과 함께.

LinearLayout-android : focusableInTouchMode = "true"에서 다음 속성을 설정하면 LinearLayout이 시작에 초점을 맞추고 활동이 하단의 EditText로 스크롤되지 않습니다.

선형 레이아웃의 하단에있는 뷰에서 requestLayout ()을 호출하면 최상위 뷰에서 포커스를 훔칠 수 있습니다. 따라서 하위 뷰에서 requestLayout ()을 호출 한 후 선형 레이아웃의 가장 위에있는 뷰에서 requestFocus ()를 호출하면됩니다.


1

fullScroll 메서드를 사용해 보셨습니까? http : //developer.android.com/reference/android/widget/ScrollView.html#fullScroll (int)

yourScrollView.fullScroll(ScrollView.FOCUS_UP);

1

Xamarin Android 개발의 경우 @Graeme 솔루션을 수행하려면 다음을 수행하십시오.

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

1

onCreate ()에서 초점을 맞출 수 없게 만드십시오.

editText.setFocusable(false);

그리고 사용

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

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

 ......
 };

다음에 조각이나 활동이 생성 될 때 Edittext로 스크롤되지 않습니다.



0

이유는 모르겠지만 실험 할 수 있습니다.

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

원하는 위치 onCreate로 수동으로 스크롤합니다.


0

scrollView.requestFocus(View.FOCUS_UP)상위 뷰가 보이기를 원할 때 내 문제를 해결했습니다. 방법에 방향 매개 변수를 지정 requestFocus(int direction)하는 것은 ScrollView위치 를 제어하는 ​​좋은 방법 입니다. 자세한 내용은 여기를 참조 하세요 .


0

제 경우 VideoView에는 ConstraintLayout내부에 ScrollView. 는 VideoView항상 초점을 훔치는되었고, 그것으로 문제가 있습니다 . 해결책은 TextureView. 바라건대 이것은 누군가에게 도움이되기를 바랍니다. 적어도 한 시간이 들었습니다. :)

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.