滚动到ScrollView中的TableLayout的最后一行

Jou*_*arc 12 android scrollview tablelayout

我希望有一个动态表,在ScrollView中使用TableLayout,随着时间的推移,用户交互会添加行.这工作正常,但是当我想使用滚动到表的末尾时fullScroll(),它总是省略最后一行; 也就是说,它滚动使得最后一个之前的那个可见.手动滚动时最后一行是可见的,滚动条也是正确的.

我当然乐于接受如何更好地布局的建议; 但我特别感兴趣的是理解为什么fullScroll()会这样做.我应该给它一个不同的参数,还是完全使用别的东西?或者它是否这样做,因为新添加的行不知何故可见?(如果是这样,我该如何解决?)或者我是否还想念其他一些明显的事情?

以下代码复制了该问题:

TestActivity.java:

package com.example.android.tests;

import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TestActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ((Button) findViewById(R.id.AddRow)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Random rnd = new Random();
                TableRow nr = new TableRow(v.getContext());
                for (int c=0; c<3; c++) {
                    TextView nv = new TextView(v.getContext());
                    nv.setText(Integer.toString(rnd.nextInt(20)-10)); 
                    nr.addView(nv);
                }
                ((TableLayout) findViewById(R.id.Table)).addView(nr);
                // Scrolls to line before last - why?
                ((ScrollView) findViewById(R.id.TableScroller)).fullScroll(View.FOCUS_DOWN);
            }
        });

    }
}
Run Code Online (Sandbox Code Playgroud)

main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <Button
    android:text="Add Row"
    android:id="@+id/AddRow"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />
  <ScrollView
    android:id="@+id/TableScroller"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/AddRow"
    android:layout_alignParentTop="true" >
    <TableLayout
      android:id="@+id/Table"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:stretchColumns="0,1,2" />
  </ScrollView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

编辑:作为参考,我实现了Romain Guy的解决方案如下:

在TestActivity.java中,替换:

            // Scrolls to line before last - why?
            ((ScrollView) findViewById(R.id.TableScroller)).fullScroll(View.FOCUS_DOWN);
Run Code Online (Sandbox Code Playgroud)

有:

            // Enqueue the scrolling to happen after the new row has been layout
            ((ScrollView) findViewById(R.id.TableScroller)).post(new Runnable() {
                public void run() {
                    ((ScrollView) findViewById(R.id.TableScroller)).fullScroll(View.FOCUS_DOWN);
                }

            });
Run Code Online (Sandbox Code Playgroud)

哪个工作正常.

Rom*_*Guy 15

在您执行fullScroll()时,布局尚未发生,因此ScrollView使用表的"旧"大小.不要立即调用fullScroll(),而是使用View.post(Runnable).