我在 Scrollview 中包含一个 TableLayout。 TableLayout 在运行时以编程方式填充。该表包含航类到达信息。一个 TableLayout 已填充,我希望能够向下滚动到当前时间段。
如果我知道我想滚动到表中 100 行中的第 40 行,我该怎么做?
这是布局的 XML。提前致谢。
<LinearLayout
android1:id="@+id/LinearLayout20"
android1:layout_width="fill_parent"
android1:layout_weight="1"
android:orientation="vertical"
android1:background="#000000" android:layout_height="10000dp" android:measureWithLargestChild="true">
<ScrollView
android1:id="@+id/scrollView1"
android1:layout_width="match_parent"
android1:layout_height="wrap_content">
<TableLayout
android1:id="@+id/arrivalstable"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content">
</TableLayout>
</ScrollView>
</LinearLayout>
最佳答案
这可能是一个迟到的答案,但希望它可以帮助别人。如果你更密切地关注Randy's link ,你会看到你需要将 ScrollView 放在一个可运行的中,否则 child.getTop() 将始终产生 0。
final ScrollView sv = (ScrollView) findViewById(R.id.scrollView1);
TableLayout tl = (TableLayout) findViewById(R.id.arrivalstable);
int index = 40;
final View child = tl.getChildAt(index);
new Handler().post(new Runnable() {
@Override
public void run() {
sv.smoothScrollTo(0, child.getBottom());
}
});
关于android - 以编程方式滚动到表格布局中的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18499953/