2013-08-28 22 views
2

我有一個包含在ScrollView中的TableLayout。 TableLayout在運行期間以編程方式填充。該表包含航班抵達信息。一個TableLayout已經被填充,我想能夠向下滾動到當前時間段。滾動到TableLayout中的一行編程方式

如果我知道我想滾動到表中第40行的第100行,我該怎麼做?

這是佈局的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> 
+0

的[可能的複製有沒有一種方法以編程方式滾動滾動視圖到特定的編輯文本?](http://stackoverflow.com/questions/6831671/is-there-a-way-to-programmatically-scroll-a-scroll-view-to-a-specific-edit-文字) –

回答

0

到這裏看看: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()); 

基本上你只需要獲取視圖的位置,並告訴了滾動滾動到它。這需要你有一個你想要滾動的項目的引用,但你也可以動態獲取。

+0

當我嘗試這個時,child.getTop()的值是零。表中有超過100行,所以它應該找到它。此外,當我做myscrollview.getHeight()時,它也返回零。一旦桌子已經填滿了,在報告高度之前是否有延遲?該表正在AsyncTask的onPostExecute方法上填充。 – user2608461

+0

@ user2608461 - 只是推測,但它可能與您的LinearLayout的layout_height爲10000dp有關。 – Randy

+0

我的佈局沒有填滿屏幕,我在某處閱讀將佈局高度設置爲較大的dp值的地方,會對此進行分類。 – user2608461

4

這可能是一個遲到的答案,但希望它可以幫助某人。如果您更緊跟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()); 
    } 
}); 
+0

這很有幫助,在這裏工作就像一個魅力 –

相關問題