2013-05-03 112 views
0

我有一個基本的Android應用程序設置與滾動視圖和文本動態添加到它。 當我添加文本時(已經發生),我希望它滾動到底部,但如果你已經在底部,我只希望它滾動到底部,所以如果你正在閱讀的東西不只是滾動。 這是我到目前爲止。Android ScrollView只滾動到底部,如果你已經有

private void AddText(final String msg){ 
    this.runOnUiThread(new Runnable() { 
     public void run() { 
      TextView log = (TextView) findViewById(R.id.chatlog); 
      if(log.getText().equals("Loading...")){ 
       log.setText(msg); 
      }else{ 
       ScrollView scroller = (ScrollView) findViewById(R.id.scroll_container); 
       //set current scroll position 
       int scroll_pos = scroller.getScrollY(); 
       //scroll to bottom 
       scroller.fullScroll(ScrollView.FOCUS_DOWN); 
       //set bottom position 
       int scroll_bot = scroller.getScrollY(); 
       //add the text 
       log.append("\r\n" + msg); 
       //if you weren't at the bottom 
       //scroll back to where you were. 
       //This isn't working, scroll bot is the same 
       //as scroll pos. 
       if(scroll_pos != scroll_bot){ 
        scroller.scrollTo(0, scroll_pos); 
       } 
       //System.out.println("Pos: " + scroll_pos); 
       //System.out.println("Bot: " + scroll_bot); 
      } 
     } 
    }); 
} 

回答

0

這是我在仿真器和我的星系中運行的一個工作示例。 基本上有兩個按鈕可以將文本添加到底部文本視圖,具體取決於您的設備大小,只有其中的第二個可用於查看自動滾動。正在編輯的文本視圖使用ontextchangedlistener在其文本更改前檢查滾動位置,然後在文本發生更改後調用延遲(因此可以使用附加文本更新屏幕)自動滾動。

佈局XML如下:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/scrollmain"> 
<RelativeLayout 
    android:id="@+id/mainRelative" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <TextView 
     android:id="@+id/titleText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Welcome to the scrolling test application!" /> 
    <Button 
     android:id="@+id/firstTextAddButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/titleText" 
     android:text="Click me to add text to the textview below without scrolling"/> 
    <Button 
     android:id="@+id/secondTextAddButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/firstTextAddButton" 
     android:layout_marginTop="380dp" 
     android:text="Click me to add text to the textview below and scroll (if you are currently scrolled all the way to the bottom)"/> 
    <TextView 
     android:id="@+id/textToEdit" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/secondTextAddButton" 
     android:textSize="18sp" 
     android:text="Some text to get us started."/> 
     /> 
</RelativeLayout> 
    </ScrollView> 

而且該活動的代碼如下:

package code.example.scrollingontextchange; 

    import android.os.Bundle; 
    import android.os.Handler; 
    import android.app.Activity; 
    import android.text.Editable; 
    import android.text.TextWatcher; 
    import android.view.Menu; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.RelativeLayout; 
    import android.widget.ScrollView; 
    import android.widget.TextView; 

    public class MainActivity extends Activity { 

private int scroll_pos; 
private int maxScrollPosition; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    TextView tv = (TextView)findViewById(R.id.textToEdit); 
    tv.addTextChangedListener(new TextWatcher(){ 
     @Override 
     public void afterTextChanged(Editable s) {    
      if(scroll_pos == maxScrollPosition) 
      { 
       Handler h = new android.os.Handler() 
       { 
        @Override    
        public void handleMessage(android.os.Message msg) 
        {     
         switch(msg.what) 
         { 
          case 0 : 
          try { 
           Thread.sleep(500); 
          } catch (InterruptedException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 
           ScrollView scrll = (ScrollView)findViewById(R.id.scrollmain); 
           scrll.fullScroll(ScrollView.FOCUS_DOWN); 
           break;        
          default : 
           break; 
         } 
        } 
       }; 
       h.sendEmptyMessage(0);     
      } 
     } 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      ScrollView scrll = (ScrollView)findViewById(R.id.scrollmain); 
      RelativeLayout rl = (RelativeLayout)findViewById(R.id.mainRelative); 

      scroll_pos = scrll.getScrollY(); 

      maxScrollPosition = rl.getHeight() - scrll.getHeight();    
     } 
     @Override 
     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 
      // TODO Auto-generated method stub 

     }   
    }); 

    OnClickListener addTextOnClick = new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      TextView tv = (TextView)findViewById(R.id.textToEdit); 
      tv.setText(tv.getText() + "\nA long time ago, in a galaxy s far far away............"); 
     }   
    }; 

    Button b = (Button)findViewById(R.id.firstTextAddButton); 
    b.setOnClickListener(addTextOnClick); 
    Button b2 = (Button)findViewById(R.id.secondTextAddButton); 
    b2.setOnClickListener(addTextOnClick); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
    } 

希望這有助於。

+0

這沒有奏效:/ – 2013-05-04 12:09:41

+0

我認爲您需要在當時進行比較,但爲了讓您的文本在滾動前在屏幕上呈現,可以在單獨的線程中延遲更改滾動位置,否則當你調用'scroller.fullScroll(ScrollView.FOCUS_DOWN);'它會滾動到當前尚未使用文本更新的屏幕底部。如果到那時你還沒有管理它,將會在接下來的幾天嘗試併發布一些示例代碼。 – Matt 2013-05-05 16:12:11

+0

但是我想要它做的是,如果你的滾動位置在底部,當添加新文本時它會滾動到底部,如果你有點讀取smoething它只是附加它,並讓你繼續讀你是什麼。但從看到的東西來看似乎不可能。 – 2013-05-06 06:01:04

1

我發現迄今最好的解決辦法是使用scrollView.post()方法,將文本更改後調用的可運行:

final ScrollView scrollView = (ScrollView) findViewById(R.id.consoleTab); 
TextView textView = (TextView) findViewById(R.id.consoleView); 
boolean autoScroll = (textView.getBottom() - (scrollView.getHeight() + scrollView.getScrollY())) <= 0; 
textView.setText(state.getConsole().getText()); 

if (autoScroll) { 
    scrollView.post(new Runnable() { 
     public void run() { 
     scrollView.fullScroll(ScrollView.FOCUS_DOWN); 
     } 
    }); 
} 
0

不要問我爲什麼,但設置你的TextView「它的重力到bottom它確實是你想要的。

相關問題