How to get the Android TextView to scroll down to

2019-04-14 08:20发布

问题:

I have a TextView whose contents are copied from a text file. Now each time the contents of the text file is loaded into the TextView, I want it to scroll down automatically to the end. This is what that portion of my layout XML file has :

    <ScrollView
        android:id="@+id/scroller"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/command"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/header"
        android:fillViewport="true" >

        <TextView
            android:id="@+id/output"
            android:layout_width="fill_parent"
            android:layout_height="132dp"
            android:bufferType="spannable"
            android:editable="false"
            android:enabled="false"
            android:focusable="true"
            android:focusableInTouchMode="false"
            android:freezesText="true"
            android:inputType="textMultiLine"
            android:isScrollContainer="true"
            android:scrollbars="vertical"
            android:text="@string/output" >

            <requestFocus />
        </TextView>
    </ScrollView>

And this is what the function looks like :

public void displayOutput()
{
    File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard,"/Android/data/terminalemulatorlog.txt");
    StringBuilder text = new StringBuilder();
    try 
    {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;

        while ((line = br.readLine()) != null) 
        {
            text.append(line);
            text.append('\n');
        }
    }
    catch (IOException e) 
    {
        Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
    }
    TextView output=(TextView) findViewById(R.id.output);
    output.setText(text);
    ((ScrollView) findViewById(R.id.scroller)).post(new Runnable() 
    {
        public void run() 
        {
            ((ScrollView) findViewById(R.id.scroller)).fullScroll(View.FOCUS_DOWN);
        }
    });
}

Now I found a partial solution over here. Hence the last line bit of code that says :

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

But this works only the first time the text file is loaded. How do I always make the TextView scroll down to the end?

回答1:

EDIT: Use this:

    final ScrollView scroller = (ScrollView) findViewById(R.id.scroller);
    scroller.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                scroller.fullScroll(View.FOCUS_DOWN);
            }
        }
    });


回答2:

Just add to your TextView:
android:gravity="bottom" android:scrollbars = "vertical"

and set movement method:
myTextView.setMovementMethod(new ScrollingMovementMethod());