【问题标题】:Scrolling to a row in a TableLayout Programatically以编程方式滚动到 Tablayout 中的一行
【发布时间】:2013-09-01 05:42:48
【问题描述】:

我有一个包含在滚动视图中的 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>

【问题讨论】:

标签: android scroll tablelayout


【解决方案1】:

这可能是一个迟到的答案,但希望它可以帮助某人。如果你更密切地关注Randy's link,你会发现你需要将滚动视图放在一个可运行对象中,否则 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());
    }
});

【讨论】:

    【解决方案2】:

    看这里:Is there a way to programmatically scroll a scroll view to a specific edit text?

    int index = 40;
    View child = mytablelayout.getChildAt(index);
    myscrollview.scrollTo(0, child.getTop());
    

    基本上你只需要获取视图的位置并告诉滚动视图滚动到它。这需要您引用要滚动到的项目,但您也可以动态获取。

    【讨论】:

    • 当我尝试这个时,child.getTop() 的值为零。表中有超过 100 行,所以它应该找到它。此外,当我执行 myscrollview.getHeight() 时,它也返回零。填写表格后,在报告高度之前是否有延迟?该表正在 AsyncTask 的 onPostExecute 方法上填充。
    • @user2608461 - 只是推测,但它可能与您的 LinearLayout 的 layout_height 为 10000dp 有关。
    • 我的布局没有填满屏幕,我在某处读到将布局高度设置为较大的 dp 值,会对其进行排序。
    猜你喜欢
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 2011-01-15
    相关资源
    最近更新 更多