2010-06-24 3 views
4

1.6, 2.2 및 MyTouch 3G Slide (API # 7이며 Android Device Chooser에서 "2.1-Update1"로 표시됨)에서이 이상한 문제가 발생했습니다. 누구나 내가 뭘 잘못하고 있는지 설명 할 수 있다면 그것을 수정하는 방법 (또는 아마도 이것이 안드로이드 버그인지 확인하는 것), 나는 그것을 크게 고맙게 여길 것이다!TextView.setText가 스크롤링 할 ScrollView를 둘러싸고있는 이유는 무엇입니까?

내 앱의 기본 아이디어는 타이머를 시작하기 위해 버튼을 탭한 다음 다시 한 번 눌러 타이머를 중지 (일시 중지) 할 수 있다는 점에서 스톱워치를 정렬하는 것입니다. 타이머를 다시 시작하고 타이머를 일시 중지하는 사이에 다른 탭이 번갈아 표시됩니다.

위젯이 많이 포함 된 RelativelLayout이 포함 된 최상위 ScrollView가 있습니다. 첫 번째 위젯은 화면 아래쪽에있는 다른 모든 위젯을 푸시하는 거대한 버튼입니다 (누르기 쉽습니다). 이는 ScrollView (및 사용자에게 화면상의 알림)를 사용하여 나머지 입력 옵션을 사용할 수있게하려는 의도로 의도적 인 것입니다.

간단한 상태 시스템 유형 설정이 있습니다. 여기서 mState는 현재 모드입니다 (사용자가 아무 버튼이나 누르기 전에 STATE_TIMER_NOT_STARTED ... 첫 번째 누른 후 실행 후 ... 두 번째 이후에 PAUSED, 다시 ... 세 번째 이후에 실행 등).

이 모든 기능은 타이머가 실행 중일 때 사용자가 시작/중지/다시 시작 버튼을 다시 누르면 스크롤 방법이 아래로 스크롤됩니다. 나는이 명령을 내리지 않고있다. (나는 ScrollView 객체에 대한 참조조차 없다.) 왜 이런 일을하는지 모르겠다.

REPRO : 다음 샘플을 컴파일 + 실행하십시오. 앱이 시작되면 '시작 타이밍'버튼을 누릅니다. 엄지 (또는 마우스)를 사용하여 화면을 위쪽으로 터치 드래그하여 (그러면 RatingBar를 볼 수 있습니다.) 그런 다음 다시 아래로 드래그하십시오 (그러면 버튼이 완전히 화면에 나타납니다). 버튼을 탭하면 (다시 '일시 정지'라고 읽음) 조금 아래로 넘어갑니다. 스크롤하지 말라고 말하는 문장이 없기 때문에 점프/스크롤 다운해서는 안됩니다. 내가 말할 수있는 한 가까이에서 스크롤을 발생시키는 것은 setText입니다 (해당 행을 주석 처리 할 때 스크롤이 발생하지 않음).

내가 무엇을 요구하는지 : 만약 내가 뭔가 바보가되고 있다면 & 당신은 그것이 무엇인지를 지적 할 수있다. 나는 그것을 정말로 고맙게 생각한다! :) *** '터치 모드'가 패널과 위쪽을 이동할 때 마우스의 스크롤 휠을 사용할 때 (에뮬레이터에서) 나타나지 않기 때문에이 작업과 관련이 있을지 궁금합니다 (예 : 시뮬레이션 된 핑거 드래그). 나는 터치 모드에서 전체를 찾을 수없고, ScrollView 내에서 터치 모드의 초점/선택에 특별한 것을 찾을 수 없다.

이 오류가 발생했음을 확인할 수 있다면 역시 괜찮을 것이다. 불행은 회사를 사랑합니다. AHEM 내 말은, 그것이 나만의 것이 아닌지 확인하는 데 도움이되기 때문입니다. :)).

MyTestApp.java

package bug.android.scrollview; 

import android.app.Activity; 
import android.os.Bundle; 
import android.text.format.Time; 
import android.view.Display; 
import android.view.View; 
import android.view.WindowManager; 
import android.widget.Button; 
import android.widget.TextView; 

public class MyTestApp extends Activity { 

    public final static int STATE_TIMER_NOT_STARTED = 1; 
    public final static int STATE_TIMER_RUNNING = 2; 
    public final static int STATE_TIMER_PAUSED = 3; 
    private int mState; 

    Time t = new Time(); 

    private Time data = new Time(); 

    private Button btnStartStopResume; 
    private TextView lblSpacer; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.new_time_entry); 

     btnStartStopResume = (Button) findViewById(R.id.btnStartStopResume); 

     // Set the button's size so that the other info will also be visible 
     Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)) 
       .getDefaultDisplay(); 
     // This is such a hack, but the windowScroller doesn't appear to 
     // have a height at this point in the lifecycle (nor in 'onResume' :() 
     btnStartStopResume.setHeight(display.getHeight() - 200); 

     lblSpacer = (TextView) findViewById(R.id.lblSpacer); 

     reset(); 
    } 

    public void doStartStopResume(View v) { 
     if (mState == MyTestApp.STATE_TIMER_NOT_STARTED) { 

      mState = MyTestApp.STATE_TIMER_RUNNING; 

      data.setToNow(); 

     } else if (mState == MyTestApp.STATE_TIMER_RUNNING) { 
      mState = MyTestApp.STATE_TIMER_PAUSED; 

      String s = getString(R.string.add_scroll_down_to_add); 
      lblSpacer.setText(s); 
     } else if (mState == MyTestApp.STATE_TIMER_PAUSED) { 

      mState = MyTestApp.STATE_TIMER_RUNNING; 
     } 
    } 
    public void doReset(View v) { 
    } 

    public void doNewRunClick(View v) { 
    } 

    public void doAddTiming(View v) { 
    } 

    public void reset() { 
     mState = STATE_TIMER_NOT_STARTED; 
    } 
} 

new_time_entry.xml

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/windowScroller" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
> 
    <RelativeLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
    > 
     <Button 
      android:id="@+id/btnStartStopResume" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dip" 
      android:text="Start Timing" 
      android:textSize="40dp" 
      android:height="290dp" 
      android:onClick="doStartStopResume" /> 

     <TextView 
      android:id="@+id/lblSpacer" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/btnStartStopResume" 
      android:layout_centerHorizontal="true" 
      android:text="@string/add_scroll_down_for_more" /> 

     <TextView 
      android:id="@+id/lblTimeStartLabel" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/lblSpacer" 
      android:layout_alignParentLeft="true" 
      android:clickable="true" 
      android:onClick="adjustStartTime" 
      android:text="Start of this run:" 
      android:textSize="8dp" /> 

     <TextView 
      android:id="@+id/lblTimeStart" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/lblTimeStartLabel" 
      android:layout_alignParentLeft="true" 
      android:clickable="true" 
      android:onClick="adjustStartTime" 
      android:text="--:--:-- --" 
      android:textColor="#FFFFFF" 
      android:textSize="26dp" /> 

     <TextView 
      android:id="@+id/lblElapsedLabel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/lblSpacer" 
      android:layout_alignRight="@id/lblSpacer" 
      android:layout_marginRight="5dp" 
      android:text="Elapsed Time:" 
      android:textSize="8dp" /> 

     <TextView 
      android:id="@+id/lblTimeElapsed" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/lblElapsedLabel" 
      android:layout_alignRight="@id/lblSpacer" 
      android:layout_marginRight="5dp" 
      android:textColor="#99ff66" 
      android:text="-- m -- sec" 
      android:textSize="26dp" 
      android:layout_marginBottom="10dip"/> 

     <CheckBox 
      android:id="@+id/chkNewRun" 
      android:onClick="doNewRunClick" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/lblTimeElapsed" 
      android:text="This is a new run of timings" 
      android:layout_marginBottom="10dip" /> 

     <TextView 
      android:id="@+id/lblIntensity" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Intensity (1 = none 5 = max)" 
      android:layout_below="@id/chkNewRun" /> 

     <RatingBar 
      android:id="@+id/rbIntensity" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/lblIntensity" 
      android:numStars="5" 
      android:rating="2" 
      android:layout_marginBottom="5dip" /> 

     <TextView 
      android:id="@+id/lblNotes" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Notes:" 
      android:layout_below="@id/rbIntensity" /> 

     <EditText 
      android:id="@+id/txtNotes" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@android:drawable/editbox_background" 
      android:layout_below="@id/lblNotes" 
      android:layout_marginBottom="10dip" /> 


     <Button 
      android:id="@+id/btnReset" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/txtNotes" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="10dip" 
      android:text="Reset" 
      android:onClick="doReset" /> 

     <Button 
      android:id="@+id/btnOk" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/txtNotes" 
      android:layout_toRightOf="@id/btnReset" 
      android:layout_alignParentRight="true" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="10dip" 
      android:text="Add Timing To List" 
      android:onClick="doAddTiming" /> 
    </RelativeLayout> 
</ScrollView> 

strings.xml의

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <string name="app_name">Timer</string> 
    <string name="dlg_edit_timing_title">Edit A Timing</string> 
    <string name="add_scroll_down_for_more">&lt; Scroll down for more options! &gt;</string> 
    <string name="add_scroll_down_to_add">&lt; Scroll down to save this timing! &gt;</string> 
    <string name="start_timing">Start Timing\n\n</string> 
    <string name="stop_timing">Pause Timing\n\n</string> 
    <string name="resume_timing">Resume Timing\n\n</string> 
</resources> 

droidManifest.XML

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="bug.android.scrollview" 
    android:versionCode="1" 
    android:versionName="1.0"> 
<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".MyTestApp" 
     android:label="@string/app_name"> 
     <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
</activity> 
</application> 
<uses-sdk android:minSdkVersion="5" /> 
</manifest> 

UPDATE 1 : 디버거에서

 if( btnStartStopResume.isInTouchMode()) 
      Toast.makeText(this, "TOUCH MODE", 2000); 
     else 
      Toast.makeText(this, "NOT touch mode", 2000); 

다음 설정 중단 점을 추가 버튼에 관계없이 I 손가락 드래그 여부 패널 (터치 모드에서 항상 위/아래, 이상인지 확인 마우스 휠 위/아래). 따라서 두 번째 버튼을 누른 후 (즉, 앱이 '중지/일시 중지 된 타이밍'모드에있을 때) 패널을 터치 모드로 드래그하여 뒤틀린 상태에서 이상한 타이밍을 유발하는 조합입니다.

업데이트 2 : 나는 단지 그것이 EditText로 스크롤되는 것을 알아 차 렸습니다. 패널을 아래로 움직이면 EditText가 선택 항목을 얻고 Click 이벤트 후에 ScrollView는 선택 항목이있는 항목으로 다시 스크롤하는 것처럼 보입니다. 마우스 휠 접근 방식에이 문제가없는 이유를 설명합니다 (선택/포커스를 버튼으로 다시 이동시킵니다).

+0

http://stackoverflow.com/questions/5375838/scrollview-disable-focus-move/7114179 # 7114179 – yanchenko

답변

3

낡은 실이지만 누군가 나 같은 사람을 찾고있는 경우에 대비해 추가 할 것입니다. 나는 똑같은 문제가 있었지만, 초점을 없애는 것은 도움이되지 못했다. 이것이 나를 위해 마침내 해결 한 것입니다.

editText.clearFocus(); 
editText.setTextKeepState(text); 

희망이 있으면 도움이 될 것입니다. TextView docs

2

좋아, 내가 무슨 일이 일어나고 있는지에 대한 핸들을 얻고, 내가뿐만 아니라 답변으로, 여기를 게시 줄 알았는데 것을 그것을 해결하는 방법에 대한 아이디어의 충분했습니다

당신은 clearFocus를 호출하는 경우 setText를 호출하기 전에 EditText (버튼 이벤트 핸들러와이 프로그램의 '실제'버전의 스레드 핸들러를 통해 실행중인 타이머 두 개)에서 모두 예상대로 작동합니다 이상한 자동 스크롤).

이 솔루션을 사용하려면 포커스를 얻을 수있는 모든 것에 초점을 맞추어야하지만,이 솔루션은 불완전한 솔루션입니다. 계속 살펴 보겠습니다. ScrollView 도큐멘트에는 onRequestFocusInDescendants라는 이름의 메소드가 있습니다. "스크롤 뷰의 자식에서 포커스를 찾을 때 화면 밖으로 스크롤되는 것에 초점을 두지 않도록 조심해야합니다. 기본 ViewGroup 구현, 그렇지 않으면이 동작이 기본 설정되었을 수 있습니다. " 여기에 무슨 일이 일어나고 있는지 가리킬지도 모릅니다 ...

+0

감사합니다. 방금이 문제를 해결하고 내 상황에서 괜찮은 포커스를 제거하여 문제를 해결했습니다. 자신의 질문에 대한 답변을 게시 해 주셔서 감사합니다. –

0

이 페이지에서 해결책을 찾았지만 제대로 작동하지 않았습니다. 보기에서 새로 고침 단추를 클릭 할 때마다 스크롤되는 scrollview가 있습니다. 내가 끝내기 시작한 것은 버튼의 왼쪽에있는 제목을 만들어서 버튼 콜백에 초점을 맞추도록 설정하는 것이 었습니다. 내 조각 내 onCreateView의

부 :

final View view = inflater.inflate(R.layout.fragment_diagnostic, container, false); 
view.findViewById(R.id.button_refresh_bluetooth_device).setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     updateBluetoothDevice(); 
     view.findViewById(R.id.bluetooth_device_title).requestFocus(); 
    } 
}); 
view.findViewById(R.id.button_refresh_network).setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     updateNetworkSection(); 
     view.findViewById(R.id.android_network_title).requestFocus(); 
    } 
}); 

내 레이아웃의 단순화 된 버전 :

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/parentPanel" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="@dimen/alert_dialog_padding_material" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:id="@+id/topPanel" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:clipToPadding="false" 
     android:paddingLeft="@dimen/alert_dialog_padding_material" 
     android:paddingRight="@dimen/alert_dialog_padding_material" 
     android:paddingBottom="@dimen/floating_action_button_margin"> 

     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/button_refresh" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom|end" 
      android:layout_alignParentRight="true" 
      android:padding="@dimen/floating_action_button_margin" 
      android:src="@drawable/ic_fab_refresh" 
      android:contentDescription="@string/content_description_refresh" 
      app:elevation="4dp" 
      app:borderWidth="0dp" 
      app:fabSize="mini"/> 

     <TextView 
      android:id="@+id/alertTitle" 
      style="?android:attr/windowTitleStyle" 
      android:singleLine="true" 
      android:ellipsize="end" 
      android:layout_toLeftOf="@id/button_refresh" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:paddingBottom="@dimen/floating_action_button_margin" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/title_diagnostics"/> 
    </RelativeLayout> 

    <ScrollView 
     android:id="@+id/scrollView" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:clipChildren="false" 
     android:clipToPadding="false" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:minHeight="48dp" 
     android:paddingLeft="@dimen/alert_dialog_padding_material" 
     android:paddingRight="@dimen/alert_dialog_padding_material" 
     android:paddingBottom="@dimen/alert_dialog_padding_material"> 

     <TableLayout 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:clipChildren="false" 
      android:shrinkColumns="1" 
      android:stretchColumns="1,2"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center_vertical"> 

       <TextView 
        android:id="@+id/bluetooth_device_title" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:text="@string/title_bluetooth_device" 
        android:focusable="true" 
        android:focusableInTouchMode="true"/> 

       <android.support.design.widget.FloatingActionButton 
        android:id="@+id/button_refresh_bluetooth_device" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:padding="@dimen/floating_action_button_margin" 
        android:src="@drawable/ic_fab_refresh" 
        android:contentDescription="@string/content_description_refresh" 
        app:elevation="4dp" 
        app:borderWidth="0dp" 
        app:fabSize="mini"/> 
      </LinearLayout> 

      <TableRow> 

       <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginRight="5dp" 
        android:layout_gravity="center_vertical" 
        android:id="@+id/bt_connection_status_image" 
        tools:src="@drawable/ic_check" 
        android:contentDescription="@string/content_description_diagnostic_status"/> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:layout_gravity="center_vertical" 
        android:text="@string/label_state"/> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:layout_gravity="center_vertical" 
        tools:text="Connected" 
        android:id="@+id/bt_connection_status"/> 

      </TableRow> 

      <LinearLayout 
       android:id="@+id/android_network_title" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center_vertical"> 

       <TextView 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:text="@string/title_android_to_network" 
        android:focusable="true" 
        android:focusableInTouchMode="true"/> 

       <android.support.design.widget.FloatingActionButton 
        android:id="@+id/button_refresh_network" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:padding="@dimen/floating_action_button_margin" 
        android:src="@drawable/ic_fab_refresh" 
        android:contentDescription="@string/content_description_refresh" 
        app:elevation="4dp" 
        app:borderWidth="0dp" 
        app:fabSize="mini"/> 
      </LinearLayout> 
      <TableRow> 

       <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginRight="5dp" 
        android:layout_gravity="center_vertical" 
        android:id="@+id/android_network_state_image" 
        tools:src="@drawable/ic_check" 
        android:contentDescription="@string/content_description_diagnostic_status"/> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:layout_gravity="center_vertical" 
        android:text="@string/label_state"/> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:layout_gravity="center_vertical" 
        tools:text="Connected" 
        android:id="@+id/android_network_state"/> 

      </TableRow> 
     </TableLayout> 
    </ScrollView> 
</LinearLayout> 
관련 문제