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

12
我想要一个动态的表格,通过用户交互不断添加行,使用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);
            }
        });

    }
}

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>

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

在TestActivity.java中替换:

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

使用:

            // 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);
                }

            });

这很好地工作。

2个回答

15

在执行 fullScroll() 方法时,布局尚未完成,因此 ScrollView 使用的是表格的“旧”大小。不要立即调用 fullScroll(),而应使用 View.post(Runnable)。


谢谢 - 这是我怀疑的事情之一,但我真的不知道修复它会如此简单。感谢您帮助我克服对所有与线程相关的恐惧 :-) - Joubarc

2
找到上面的提示有用吗?这里有一个简单的实现,可以滚动ScrollView以使给定的子项可见...
a:准备以下辅助类。
public class ScrollToTrick implements Runnable {

ScrollView scroller;
View       child;

ScrollToTrick(ScrollView scroller, View child) {
    this.scroller=scroller; 
    this.child=child;

}
public void run() {
    scroller.scrollTo(0, child.getTop());
}
}
如何调用:按照以下方式进行调用。
my_scroller.post(new ScrollToTrick(my_scroller,child_to_scroll_to) );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接