2014-01-14 90 views
0

所以我有一个TableLayout,我想放在一个ScrollView。但我有一个问题:把一个TableLayout放在一个ScrollView中?

TableLayout最初是空的,我编程添加TableRows它通过点击一个按钮。这工作正常,如果我把TableLayout正常ScrollView。我想把TableLayout放在ScrollView的底部。所以每次添加TableRow时,最后一个单元格将始终与父滚动视图的底部对齐。 (有点像聊天应用程序的工作方式 - 当你按下发送时,你的消息被添加到底部,而所有其他消息被推高)。

发生什么事是,如果我使用android:layout_gravity="bottom"试图实现这一点,我无法看到从屏幕视图向上推动的任何行(我无法向上滚动)。但是,由于某种原因,我可以向下滚动到空虚,这应该是不可能的,因为最后的TableRow应该在底部。

基本上问题是我可以向下滚动没有TableRows,但我不能向上滚动到哪里。

这是相关的XML代码:

<ScrollView 
    android:id="@+id/tableScrollView" 
    android:layout_width="match_parent" 
    android:layout_weight="0.7" 
    android:layout_height="0dip" 
    android:scrollbars="none" 
    android:orientation="vertical" 
    android:background="@drawable/stripes_background" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_gravity="bottom">  

     <TableLayout 
      android:id="@+id/outputTable" 
      android:layout_alignParentBottom="true" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="10dp" 
      android:divider="@drawable/table_divider" 
      android:showDividers="middle">  
    </TableLayout> 
    </RelativeLayout> 

</ScrollView> 

回答

4

它看起来像你应该使用ListView或其某种变体;它为与滚动内容相关的各种优化提供支持。看看可用于自定义ListView实现的一些教程和开发人员文档。

编辑查看问题Listview Scroll to the end of the list after updating the list了解使用ListView执行滚动操作的详细信息。

+0

哦,我意识到ListView的存在只有在我为TableView编写它所有的时候才存在......我只是有点懒得改变它,但现在我会读它。 – ujvl

+0

你可能会发现它是一个有价值的重构。 – hunt