Can solve this problem with ScrollView
same.
I have an Activity where I use a ScrollView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<android.support.v7.widget.Toolbar
android:id="@+id/tb_pergunta"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize" />
<TableLayout
android:id="@+id/tl_principal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
<ScrollView
android:id="@+id/sv_table"
android:layout_height="700dp"
android:scrollbars="horizontal|vertical"
android:layout_width="match_parent"
android:layout_marginTop="5dip"
android:scrollbarStyle="outsideInset"
android:fillViewport="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:id="@+id/myLayout"
android:layout_height="match_parent"
android:layout_weight="1">
</LinearLayout>
</ScrollView>
</LinearLayout>
Within LinearLayout
of ScrollView
I assign "n" Layouts created dynamically. So with that, I do not have a layout set to place within ScrollView
.
These dynamic layouts also receive children of various types of widgets ( RadioButton
, CheckBox
, EditText
, TextView
, etc).
Because they are dynamically created, they do not have a right position within the LinearLayout
parent. With that, I had a hard time rolling down ScrollView
to those children.
Getting the position of the parent Layouts was easy, as it was only by getting the childAt (index) from the LinearLayout
parent (which is within ScrollView
).
posicaoPai = lnPrincipal.getTop();
But until you roll into the position of the children, I have sifted.
Finally. The way I found to scroll up to the children was to get the top position of the "lnPai.getTop ()" and the top position of the children that are inside it.
posicaoFilho = lnFilho.getTop();
Add the position of the father with the position of the child:
posicaoScrol = posicaoPai + posicaoFilho;
And with that I got the position of the child within ScrollView
that would equal the Top position of the parent Layout with the top position of the child.
After this, just move to the ScrollView
position of the child that was found in the sum above.
final int posicaoArray = arrayFilhoOpcao.get(contArray_z + 1);
myScrollLayout.post(new Runnable() {
@Override
public void run() {
myScrollLayout.smoothScrollTo(0, posicaoArray);
}
});
The ArrayList
I created to put all the positions of the children of the Layout that I'm currently browsing.
Well ... I hope it works for someone and if it seems silly to others, many beginners like me sometimes have a hard time finding solutions to problems that would be simple for others.