I have the following structure in my UI
<ScrollView
android:id="@+id/vscroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
I fill the TableLayout with TableRows programmatically.
Sometimes I need to add rows at the top of the table (insert before child(0)).
And the problem is the ScrollView jumps up.
I can tell you that I Logged the ScrollY() position of the ScrollView before and after the row insertion. And it remains the same (!!)
This is what is causing the jump, because now I have some more rows and scroll position Y= 234 (for example) before was on row A and now the same position Y means row Q.
Row A was the (lt’s say) fifth row, and now after insertion row Q is the fifth row
So you see, from the ScrollView point of view nothing changed, but the View shows now a different Row.
If at this point you are not totally confused with my explanation, then maybe you can advise how can I insert rows at the top without changing what I see on screen.
In other words I want the insertion to go stealth without any influence on what the user sees.
If some code will help clear the picture, here is how I insert 5 rows at the top
table = (TableLayout) findViewById(R.id.table);
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
for(int i=0; i<5 ; i++) {
TableRow mr = new TableRow(getContext());
// some stuff to set the row texts
table.addView(mr, 0, params);
}
Before Insertion After Insertion
Row 0 New Row 0
Row 1 New Row 1
Row 2 New Row 2
Row 3 New Row 3 <--- vscroll Y position stays the same
Row 4 New Row 4
Row 5 Old Row 0
Row 6 Old Row 1
Row 7 Old Row 2
Row 8 Old Row 3 (vscroll jumps up from here to new Row 3)
Row 9 Old Row 4
Row 10 Old Row 5
Row 11 Old Row 6
I haven’t tested it but you could try to
post()aRunnablewhere you would get the newly inserted row’s height and then scroll theScrollViewby that amount so it stays at the same position: